"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
feat(tools): add SEC 13F institutional holdings
Haozhe Wu375b833ee323
diff --git a/agent/src/tools/institutional_holdings_tool.py b/agent/src/tools/institutional_holdings_tool.pynew file mode 100644index 000000000..22f42b776--- /dev/null+++ b/agent/src/tools/institutional_holdings_tool.py@@ -0,0 +1,2288 @@+"""Read-only tool: SEC Form 13F-HR institutional holdings from EDGAR.++Every institutional investment manager exercising discretion over more than+$100M of U.S. exchange-traded equities must file Form 13F-HR within 45 days of+each calendar quarter end. The holdings themselves live in a separate+INFORMATION TABLE document inside the filing, not in the primary document.+Three free, no-auth SEC endpoints carry everything this tool needs:++* ``https://efts.sec.gov/LATEST/search-index`` — EDGAR full-text search. Beyond+ free text (``q``) it accepts ``forms``, ``ciks``, ``entityName``, ``startdt``,+ ``enddt`` and ``from``, and each hit's ``_source`` carries ``ciks``,+ ``display_names``, ``form``, ``file_date``, ``period_ending``, ``adsh`` and —+ crucially — ``file_type``, which is literally ``"INFORMATION TABLE"`` for the+ holdings document. Because the info table is indexed as text, searching a+ CUSIP finds every manager that reported it. Coverage starts in 2001; the+ result window is hard-capped at ``from + size <= 10000``.+* ``https://www.sec.gov/Archives/edgar/data/<cik>/<accession_no_dashes>/index.json``+ — the document list for one filing, with per-file byte sizes. The information+ table's filename is filer-chosen (``infotable.xml``, ``53405.xml``,+ ``MJPinftable.xml`` have all been observed), so it is discovered here and then+ confirmed by its XML root tag rather than guessed.+* ``https://data.sec.gov/submissions/CIK##########.json`` — the per-company+ filing index, used as a fallback when full-text search is unavailable.++Information-table XML (namespace+``http://www.sec.gov/edgar/document/thirteenf/informationtable``) repeats one+``<infoTable>`` per position with ``nameOfIssuer``, ``titleOfClass``, ``cusip``,+``value``, ``shrsOrPrnAmt/{sshPrnamt,sshPrnamtType}``, ``putCall`` (options+only), ``investmentDiscretion``, ``otherManager`` and+``votingAuthority/{Sole,Shared,None}``. A manager that splits discretion across+sub-advisers emits **several rows for the same CUSIP** (Berkshire Hathaway's+2026-03-31 table carries four separate ALLY FINL INC rows), so rows are+aggregated by CUSIP before anything is ranked.++A quarter is a *chain of filings*, not one filing. ``primary_doc.xml`` carries+``formData/coverPage/isAmendment``, ``amendmentNo`` and+``amendmentInfo/amendmentType``, and the amendment type decides how the+information table relates to what came before. Measured over four full SEC+Form 13F data sets (2016Q1, 2020Q3, 2023Q2 and the 2026-03-01..2026-05-31 set —+about 40,000 cover pages) ``amendmentType`` takes exactly two values,+``RESTATEMENT`` and ``NEW HOLDINGS``, and nothing else. DZ BANK's 2020-09-30+quarter is the whole rule in one filer: the original (1,851 rows,+$41.70B) was replaced by a ``RESTATEMENT`` (1,776 rows, $41.68B) and then+*extended* by a ``NEW HOLDINGS`` amendment carrying 2 rows and $1.17M whose own+cover page says it exists "to add new holdings ... that were inadvertently+omitted from the original Form 13F". Reading only the original, only the newest,+or only the largest document all give a different and wrong answer.++Four gotchas this module compensates for, each verified against live filings:++1. **The value column changed units.** 13F ``value`` used to be reported in+ thousands and is now reported in whole dollars. The changeover is not clean:+ Berkshire's table filed 2022-11-14 totals 296,096,640 (thousands) while its+ 2023-02-14 table totals 299,007,622,119 (dollars), yet BancFirst Trust &+ Investment Management filed on 2023-01-03 with an implied AAPL price of+ $0.1296 — still thousands — while Rede Wealth filed 2023-01-17 at $129.93.+ A pure filing-date rule is therefore wrong by 1000x for some filings. The+ filing date only sets a *prior*; the decision is confirmed against the median+ implied price (value / shares) of the share-denominated rows, and both the+ chosen unit and the basis for choosing it are reported.+2. **Pre-2013 filings have no XML at all.** Berkshire's 2011-11-14 filing+ contains one plain-text document (``d254132d13fhr.txt``). Those filings are+ reported as unparseable rather than silently returned empty.+3. **The form suffix and the cover page disagree, in both directions.** In the+ 2026-03-01..2026-05-31 data set 8 filings are submitted as ``13F-HR/A`` while+ their cover page carries no ``isAmendment`` and no ``amendmentType`` at all+ (AEGON ASSET MANAGEMENT UK Plc, eight quarters in one batch — confirmed+ against the raw ``primary_doc.xml``), and 1 filing is submitted as plain+ ``13F-HR`` while its cover page declares ``isAmendment=true``,+ ``amendmentNo=1``, ``NEW HOLDINGS`` (Nova Wealth Management). So neither+ signal alone classifies a filing: a document is treated as an amendment when+ *either* says so, and an amendment whose type is absent or outside the known+ set is reported as unresolved rather than assumed to restate or to extend.+4. **SEC rate-limits by source IP** and asks for a contact ``User-Agent``.+ Every request here shares the ``"sec"`` throttle bucket and the User-Agent of+ :mod:`backtest.loaders.sec_edgar_client`, so this tool cannot burst past the+ other SEC callers in the process. Bursting without spacing does fail in+ practice — an unthrottled probe loop got a payload with no ``hits`` key back+ from full-text search.++Filer discovery is windowed on the statutory deadline rather than on a flat+lag. Full-text search answers newest-first, so a window running 120 days past+quarter end starts inside the *next* quarter's filing season: for target+2026-03-31 the first page of ``2026-04-01..2026-07-29`` was 97/100 filings for+2026-06-30 and only 1 for the target, all of which were then discarded.+Searching ``2026-04-01..2026-05-15`` — quarter end through the 45-day deadline —+returns 96/100 on target instead. The late tail is a second, separate window so+nothing that the flat lag used to reach is lost.++Ticker -> CUSIP has no SEC lookup table, so it is resolved from the SEC's own+"fails to deliver" data (``SETTLEMENT DATE|CUSIP|SYMBOL|QUANTITY (FAILS)|+DESCRIPTION|PRICE``), which is authoritative but only lists securities that had+a settlement failure in the covered half-month — 13,025 distinct symbols in the+2026-07-01..2026-07-15 file. Anything absent must be passed as an explicit+``cusip``; no mapping is ever guessed from an issuer name.++Markets: United States only.+"""++from __future__ import annotations++import io+import json+import logging+import os+import re+import threading+import time+import xml.etree.ElementTree as ET+import zipfile+from datetime import date, timedelta+from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple++from backtest.loaders._http import throttled_get+from backtest.loaders.sec_edgar_client import get_submissions++# The SEC User-Agent and the throttle floor are deliberately imported from the+# shared client rather than restated: one contact string, one rate limit, one+# place to change them. Both are stable module-level helpers.+from backtest.loaders.sec_edgar_client import _min_interval, _pad_cik, _user_agent+from src.agent.tools import BaseTool+from src.config.limits import TOOL_RESULT_LIMIT+from src.tools._result_paging import fit_records++logger = logging.getLogger(__name__)++_HOST_KEY = "sec"+_FTS_URL = "https://efts.sec.gov/LATEST/search-index"+_ARCHIVES = "https://www.sec.gov/Archives/edgar/data"+_FTD_INDEX_URL = "https://www.sec.gov/data/foiadocsfailsdatahtm"++_MODES = ("manager_holdings", "ticker_holders", "top_managers")++# EDGAR full-text search returns 100 hits per page and refuses from + size > 10000.+_FTS_PAGE = 100+_FTS_MAX_FROM = 9900++# Defensive caps. Each keeps one call bounded in both wall-clock and characters.+_MAX_TOP_N = 100+_DEFAULT_TOP_N = 25+_MAX_HOLDER_SAMPLE = 25+_DEFAULT_HOLDER_SAMPLE = 10+_MAX_SCAN = 300+# Each scanned filing is its own SEC round trip and EDGAR answers in roughly a+# second, so a scan of 100 would overrun the default wall-clock budget and come+# back short. 30 fits comfortably inside it.+_DEFAULT_SCAN = 30+_MAX_INFOTABLE_ROWS = 20_000+_MAX_CHANGES = 200+# Share of the tool-result budget the quarter-over-quarter change list may take.+# It sits in the fixed part of the envelope, outside the paged position list, so+# it has to be bounded on its own or it starves and then overflows the result.+_CHANGES_BUDGET_SHARE = 0.5+# A filing's document list can name several XML files. Candidates are ordered by+# how much the filename looks like an information table before any of them is+# fetched, so the cap only bites on filings that carry many decoys. Across 150+# sampled live filings (105 from 2026, 45 from 2016) every single one carried+# exactly one non-``primary_doc.xml`` XML document, so this ceiling is defensive+# rather than something observed to be hit.+_MAX_TABLE_CANDIDATES = 8+# Filenames containing any of these are probed before anything else.+_TABLE_NAME_HINTS = ("infotable", "informationtable", "inftable", "information", "table", "info")++# 13F reporting began in 1978 but EDGAR electronic filing (and therefore+# anything reachable here) starts in the 1990s.+_MIN_QUARTER_YEAR = 1993+# Managers have 45 days after quarter end to file; 46 is the first day the+# quarter can be assumed populated, and 120 days covers late amendments.+_FILING_LAG_DAYS = 46+_FILING_DEADLINE_DAYS = 45+_FILING_WINDOW_DAYS = 120+_QUARTER_ENDS = {1: (3, 31), 2: (6, 30), 3: (9, 30), 4: (12, 31)}++# Cover-page amendment vocabulary. These are the only two values `amendmentType`+# took across the four SEC Form 13F data sets sampled in the module docstring;+# anything else (including a blank) is deliberately NOT mapped to a behaviour.+_ROLE_ORIGINAL = "original"+_ROLE_RESTATEMENT = "restatement"+_ROLE_NEW_HOLDINGS = "new_holdings"+_ROLE_UNSPECIFIED = "unspecified_amendment"+_AMENDMENT_ROLES = {"RESTATEMENT": _ROLE_RESTATEMENT, "NEW HOLDINGS": _ROLE_NEW_HOLDINGS}+# What bounds this is filings per filer-quarter, not `amendmentNo`: across the+# 8,933 filer-quarters in the 2023q2 Form 13F data set the busiest filer made 7+# filings for one quarter and none made more than 7, so 12 leaves room without+# letting one pathological filer turn into an unbounded scan. `amendmentNo` is+# NOT a proxy for that count — it is filer-entered and reaches 99 (STATE STREET+# CORP, 0000093751-23-000581) in that same data set.+_MAX_CHAIN_FILINGS = 12+_TRUE_TEXT = {"TRUE", "T", "Y", "YES", "1"}+_FALSE_TEXT = {"FALSE", "F", "N", "NO", "0"}++# Whole-dollar reporting took over around the start of 2023; see the module+# docstring for why this is only a prior and never the final word.+_DOLLAR_UNITS_FROM = "2023-01-03"+# Median implied price (value / shares) below this while expecting dollars means+# the filing is really in thousands; above the upper bound while expecting+# thousands means the opposite. Real equity portfolios sit between the two.+_IMPLIED_PRICE_MIN_USD = 1.0+_IMPLIED_PRICE_MAX_THOUSANDS = 3.0+_MIN_ROWS_FOR_UNIT_CHECK = 5++# A manager only has to file 13F once it exercises discretion over more than+# $100M of section 13(f) securities, so a cover-page total that lands *below*+# that under the whole-dollar prior is far more likely still denominated in+# thousands than it is a genuine sub-threshold portfolio. Measured against 40+# consecutive 2026-06-30 filers, 2 of them (CapitalatWork S.A., Eurizon Asset+# Management Slovakia — both non-U.S. managers) report exactly this way, so the+# prior misreads roughly 5% of filers by 1000x. Because that error *shrinks* a+# portfolio it silently drops real managers out of a value ranking, so the+# ambiguous minority is resolved against the filing's own information table+# instead of being guessed either way.+_FILING_THRESHOLD_USD = 100_000_000.0++_ENV_MAX_XML_MB = "VIBE_TRADING_SEC_13F_MAX_XML_MB"+_ENV_BUDGET_S = "VIBE_TRADING_SEC_13F_BUDGET_S"+_ENV_FTD_URL = "VIBE_TRADING_SEC_FTD_URL"+_ENV_FTD_FILES = "VIBE_TRADING_SEC_FTD_FILES"++_DEFAULT_MAX_XML_MB = 25.0+_DEFAULT_BUDGET_S = 120.0+_DEFAULT_FTD_FILES = 1+_MAX_FTD_FILES = 6++# Process-wide memoized ticker -> (cusip, description) map, built lazily and+# only when a ticker actually has to be resolved.+_CUSIP_CACHE: Optional[Dict[str, Tuple[str, str]]] = None+_CUSIP_CACHE_SOURCES: List[str] = []+_CUSIP_CACHE_LOCK = threading.Lock()+++# --------------------------------------------------------------------------- #+# HTTP+# --------------------------------------------------------------------------- #+++def _sec_get(url: str, params: Optional[Dict[str, Any]] = None) -> Any:+ """GET ``url`` through the shared SEC throttle bucket.++ Args:+ url: Fully-qualified SEC endpoint.+ params: Optional query parameters.++ Returns:+ The :class:`requests.Response`, already ``raise_for_status``-checked.++ Raises:+ requests.RequestException: On a non-2xx status.+ """+ response = throttled_get(+ url,+ host_key=_HOST_KEY,+ min_interval=_min_interval(),+ params=params,+ headers={"User-Agent": _user_agent()},+ timeout=30.0,+ )+ response.raise_for_status()+ return response+++def _fts(params: Dict[str, Any]) -> Dict[str, Any]:+ """Run one EDGAR full-text search and return its ``hits`` block.++ Args:+ params: Query parameters (``q`` / ``forms`` / ``ciks`` / ``entityName`` /+ ``startdt`` / ``enddt`` / ``from``).++ Returns:+ ``{"total": int, "total_is_lower_bound": bool, "hits": [source-dicts+ with an extra "_id" key]}``. Elasticsearch stops counting at 10,000 and+ reports ``relation: "gte"``, so a total at that ceiling is a floor and+ must never be presented as an exact count.++ Raises:+ RuntimeError: When the response carries no ``hits`` block — full-text+ search answers HTTP 200 with an error document when it is unhappy.+ requests.RequestException: On transport failure.+ """+ payload = _sec_get(_FTS_URL, {"q": "", **params}).json()+ hits = payload.get("hits") if isinstance(payload, dict) else None+ if not isinstance(hits, dict):+ message = payload.get("errorMessage") if isinstance(payload, dict) else None+ raise RuntimeError(f"EDGAR full-text search returned no hits block: {message or payload}")+ rows: List[Dict[str, Any]] = []+ for hit in hits.get("hits") or []:+ source = hit.get("_source") if isinstance(hit, dict) else None+ if isinstance(source, dict):+ rows.append({**source, "_id": hit.get("_id")})+ total = hits.get("total") if isinstance(hits.get("total"), dict) else {}+ return {+ "total": int(total.get("value") or 0),+ "total_is_lower_bound": total.get("relation") == "gte",+ "hits": rows,+ }+++def _env_float(name: str, default: float) -> float:+ """Read a positive float env override, falling back to ``default``."""+ raw = os.getenv(name, "")+ try:+ value = float(raw)+ except (TypeError, ValueError):+ return default+ return value if value > 0 else default+++def _max_xml_bytes() -> int:+ """Return the per-document byte ceiling for an information table."""+ return int(_env_float(_ENV_MAX_XML_MB, _DEFAULT_MAX_XML_MB) * 1024 * 1024)+++class _Budget:+ """Monotonic wall-clock guard so a wide scan stops instead of grinding on."""++ def __init__(self) -> None:+ self.deadline = time.monotonic() + _env_float(_ENV_BUDGET_S, _DEFAULT_BUDGET_S)+ self.exhausted = False++ def ok(self) -> bool:+ """Return ``True`` while budget remains, latching ``exhausted`` when not."""+ if time.monotonic() >= self.deadline:+ self.exhausted = True+ return False+ return True+++# --------------------------------------------------------------------------- #+# Quarter handling+# --------------------------------------------------------------------------- #+++def _parse_quarter(value: Any) -> str:+ """Normalize a quarter argument to the ``YYYY-MM-DD`` calendar quarter end.++ Accepts ``"2026Q1"``, ``"2026-q1"``, ``"2026-03-31"`` and ``"2026-3-31"``.++ Args:+ value: The raw ``quarter`` argument.++ Returns:+ The quarter-end date as ``YYYY-MM-DD``.++ Raises:+ ValueError: When the text is not a recognizable quarter, the year is+ outside ``1993..current+1``, or the date is not a quarter end.+ """+ text = str(value).strip().upper().replace(" ", "")+ match = re.fullmatch(r"(\d{4})-?Q([1-4])", text)+ if match:+ year, quarter = int(match.group(1)), int(match.group(2))+ else:+ match = re.fullmatch(r"(\d{4})-(\d{1,2})-(\d{1,2})", text)+ if not match:+ raise ValueError(+ f"quarter {value!r} is not recognizable; use '2026Q1' or a quarter-end date '2026-03-31'"+ )+ year, month, day = (int(g) for g in match.groups())+ quarter = next((q for q, md in _QUARTER_ENDS.items() if md == (month, day)), None)+ if quarter is None:+ raise ValueError(+ f"quarter {value!r} is not a calendar quarter end; use 03-31, 06-30, 09-30 or 12-31"+ )+ if not _MIN_QUARTER_YEAR <= year <= date.today().year + 1:+ raise ValueError(f"quarter year {year} is outside {_MIN_QUARTER_YEAR}..{date.today().year + 1}")+ month, day = _QUARTER_ENDS[quarter]+ return f"{year:04d}-{month:02d}-{day:02d}"+++def _quarter_label(period_end: Optional[str]) -> Optional[str]:+ """Render a ``YYYY-MM-DD`` quarter end as ``YYYYQn``, or ``None`` if unusable."""+ if not period_end or len(str(period_end)) < 7:+ return None+ text = str(period_end)+ try:+ year, month = int(text[:4]), int(text[5:7])+ except ValueError:+ return None+ quarter = next((q for q, md in _QUARTER_ENDS.items() if md[0] == month), None)+ return f"{year}Q{quarter}" if quarter else None+++def _latest_reportable_quarter(today: Optional[date] = None) -> str:+ """Return the newest quarter end whose 45-day filing deadline has passed."""+ cutoff = (today or date.today()) - timedelta(days=_FILING_LAG_DAYS)+ year = cutoff.year+ for quarter in (4, 3, 2, 1):+ month, day = _QUARTER_ENDS[quarter]+ if date(year, month, day) <= cutoff:+ return f"{year:04d}-{month:02d}-{day:02d}"+ return f"{year - 1:04d}-12-31"+++def _discovery_windows(period_end: str) -> List[Tuple[str, str]]:+ """Return the full-text-search windows for a quarter, densest first.++ Full-text search sorts newest-first, so one flat 120-day window spends its+ early pages inside the following quarter's filing season and throws almost+ all of them away — measured live at 3 usable hits out of the first 100 for+ target 2026-03-31. Splitting the same span at the 45-day statutory deadline+ puts the filing season itself in the first window (96/100 on target) and+ leaves the late tail as a second window that is only paged when the first+ one has not filled the sample. The union is exactly the old window, so no+ filing that used to be reachable stops being reachable.++ Args:+ period_end: Quarter end as ``YYYY-MM-DD``.++ Returns:+ ``[(startdt, enddt), ...]`` from the deadline window to the late tail.+ """+ end = date.fromisoformat(period_end)++ def day(offset: int) -> str:+ return (end + timedelta(days=offset)).isoformat()++ return [(day(1), day(_FILING_DEADLINE_DAYS)), (day(_FILING_DEADLINE_DAYS + 1), day(_FILING_WINDOW_DAYS))]+++# --------------------------------------------------------------------------- #+# Ticker -> CUSIP (SEC fails-to-deliver data)+# --------------------------------------------------------------------------- #+++def _ftd_urls() -> List[str]:+ """Discover the most recent SEC fails-to-deliver archive URLs.++ The FOIA landing page is scraped rather than the filenames being computed,+ because the SEC has served the same series from two different directories+ (``/files/data/fails-deliver-data/`` and ``/files/data/other/fails-deliver-data/``)+ and only the page knows which is which.++ Returns:+ Newest-first absolute ``.zip`` URLs, at most the configured file count.++ Raises:+ requests.RequestException: If the landing page cannot be fetched.+ RuntimeError: If the page lists no archives.+ """+ pinned = os.getenv(_ENV_FTD_URL, "").strip()+ if pinned:+ return [pinned]+ try:+ wanted = max(1, min(int(os.getenv(_ENV_FTD_FILES, "")), _MAX_FTD_FILES))+ except (TypeError, ValueError):+ wanted = _DEFAULT_FTD_FILES+ html = _sec_get(_FTD_INDEX_URL).text+ hrefs = re.findall(r'href="(/[^"]*cnsfails\d{6}[ab]\.zip)"', html)+ if not hrefs:+ raise RuntimeError("SEC fails-to-deliver index listed no archives")+ ordered = sorted(dict.fromkeys(hrefs), key=lambda h: h.rsplit("/", 1)[-1], reverse=True)+ return [f"https://www.sec.gov{h}" for h in ordered[:wanted]]+++def _load_ftd_map(url: str, into: Dict[str, Tuple[str, str]]) -> None:+ """Merge one fails-to-deliver archive into ``into`` as ticker -> (cusip, description).++ The pipe-delimited payload is ``SETTLEMENT DATE|CUSIP|SYMBOL|QUANTITY+ (FAILS)|DESCRIPTION|PRICE``. Earlier entries win, so a newer archive passed+ first keeps precedence over an older one.++ Args:+ url: Absolute URL of a ``cnsfailsYYYYMM{a,b}.zip`` archive.+ into: Accumulator mutated in place.++ Raises:+ requests.RequestException: On transport failure.+ zipfile.BadZipFile: If the payload is not a zip archive.+ """+ archive = zipfile.ZipFile(io.BytesIO(_sec_get(url).content))+ for member in archive.namelist():+ for line in archive.read(member).decode("utf-8", "replace").splitlines()[1:]:+ parts = line.split("|")+ if len(parts) < 5:+ continue+ symbol, cusip = parts[2].strip().upper(), parts[1].strip().upper()+ if symbol and cusip and symbol not in into:+ into[symbol] = (cusip, parts[4].strip())+++def _cusip_map() -> Tuple[Dict[str, Tuple[str, str]], List[str]]:+ """Return the memoized ticker -> (cusip, description) map and its source URLs.++ Raises:+ Exception: Any fetch/parse failure from the first successful build+ attempt is propagated so the caller can report it verbatim.+ """+ global _CUSIP_CACHE+ if _CUSIP_CACHE is not None:+ return _CUSIP_CACHE, _CUSIP_CACHE_SOURCES+ with _CUSIP_CACHE_LOCK:+ if _CUSIP_CACHE is None:+ urls = _ftd_urls()+ mapping: Dict[str, Tuple[str, str]] = {}+ for url in urls:+ _load_ftd_map(url, mapping)+ _CUSIP_CACHE = mapping+ _CUSIP_CACHE_SOURCES[:] = urls+ return _CUSIP_CACHE, _CUSIP_CACHE_SOURCES+++def _cusip_for_ticker(ticker: str) -> Tuple[Optional[str], Optional[str], List[str]]:+ """Resolve a ticker to a CUSIP via SEC fails-to-deliver data.++ The fails-to-deliver feed writes share classes without a separator+ (Berkshire class B is ``BRKB``, not ``BRK.B``), so the separator-stripped+ form is tried as well. Nothing is ever inferred from an issuer name.++ Args:+ ticker: A U.S. ticker, case-insensitive.++ Returns:+ ``(cusip, description, source_urls)``; ``cusip`` is ``None`` when the+ symbol is absent from the covered half-month.++ Raises:+ Exception: Propagated from the one-time map build.+ """+ mapping, sources = _cusip_map()+ candidate = ticker.strip().upper()+ for key in (candidate, candidate.replace(".", "").replace("-", "")):+ if key in mapping:+ cusip, description = mapping[key]+ return cusip, description, sources+ return None, None, sources+++# --------------------------------------------------------------------------- #+# Filing discovery+# --------------------------------------------------------------------------- #+++def _looks_like_cik(text: str) -> bool:+ """Return ``True`` when ``text`` is a bare (optionally ``CIK``-prefixed) number."""+ return bool(re.fullmatch(r"(?:CIK)?[\s#-]*\d{1,10}", text.strip().upper()))+++def _resolve_manager(manager: str) -> Tuple[str, Optional[str], List[Dict[str, str]]]:+ """Resolve a manager name or CIK to a padded CIK.++ Args:+ manager: A 13F filer name (e.g. ``"Bridgewater Associates"``) or a CIK.++ Returns:+ ``(padded_cik, display_name, other_candidates)``. ``other_candidates``+ lists the further name matches so an ambiguous name is visible rather+ than silently collapsed to one filer.++ Raises:+ ValueError: When the name matches no 13F filer.+ RuntimeError: When full-text search returns an unusable payload.+ requests.RequestException: On transport failure.+ """+ if _looks_like_cik(manager):+ return _pad_cik(manager), None, []+ result = _fts({"forms": "13F-HR", "entityName": manager.strip()})+ seen: Dict[str, str] = {}+ for hit in result["hits"]:+ for cik in hit.get("ciks") or []:+ if cik not in seen:+ seen[cik] = (hit.get("display_names") or [""])[0]+ if not seen:+ raise ValueError(f"no 13F-HR filer matched manager name {manager!r}")+ items = list(seen.items())+ primary_cik, primary_name = items[0]+ others = [{"cik": c, "name": n} for c, n in items[1:6]]+ return _pad_cik(primary_cik), primary_name or None, others+++def _list_13f_filings(cik: str, limit: int = 200) -> List[Dict[str, Any]]:+ """List a filer's 13F-HR filings, newest first.++ Full-text search is preferred because it reports ``period_ending`` directly+ and reaches further back than the submissions "recent" block; the+ submissions document is the fallback.++ The limit is deliberately generous because the result is truncated by+ *filing date* while callers group it by *reporting period*, and amendments+ arrive years late: DZ BANK amended its 2020-09-30 quarter on 2026-05-04, so+ a 40-filing cut kept the two 2026 amendments and dropped the 2020 original+ they amend — half of that quarter's chain, silently.++ Args:+ cik: Padded 10-digit CIK.+ limit: Maximum filings to return.++ Returns:+ Dicts of ``{accession, form, filing_date, period_end, is_amendment}``,+ deduplicated by accession and sorted newest filing date first.+ """+ filings: List[Dict[str, Any]] = []+ try:+ for hit in _fts({"forms": "13F-HR", "ciks": cik})["hits"]:+ accession = hit.get("adsh")+ if not accession:+ continue+ form = str(hit.get("form") or "")+ filings.append(+ {+ "accession": accession,+ "form": form,+ "filing_date": hit.get("file_date"),+ "period_end": hit.get("period_ending"),+ "is_amendment": form.upper().endswith("/A"),+ }+ )+ except Exception as exc: # noqa: BLE001 - full-text search is the preferred path, not the only one+ logger.warning("13F full-text filing index failed for CIK %s: %s", cik, exc)++ if not filings:+ recent = ((get_submissions(cik).get("filings") or {}).get("recent")) or {}+ forms = recent.get("form") or []+ for idx, raw_form in enumerate(forms):+ form = str(raw_form or "")+ if not form.upper().startswith("13F-HR"):+ continue+ filings.append(+ {+ "accession": _at(recent.get("accessionNumber"), idx),+ "form": form,+ "filing_date": _at(recent.get("filingDate"), idx),+ "period_end": _at(recent.get("reportDate"), idx),+ "is_amendment": form.upper().endswith("/A"),+ }+ )++ unique: Dict[str, Dict[str, Any]] = {}+ for filing in filings:+ if filing["accession"] and filing["accession"] not in unique:+ unique[filing["accession"]] = filing+ ordered = sorted(unique.values(), key=lambda f: str(f.get("filing_date") or ""), reverse=True)+ return ordered[:limit]+++def _target_period(filings: List[Dict[str, Any]], period_end: Optional[str]) -> Optional[str]:+ """Resolve the quarter to read, defaulting to the newest one indexed.++ Args:+ filings: Filings from :func:`_list_13f_filings`.+ period_end: Explicit quarter end, or ``None`` to take the newest.++ Returns:+ The quarter end as ``YYYY-MM-DD``, or ``None`` when no filing carries+ one (the submissions fallback can omit ``reportDate``).+ """+ if period_end:+ return period_end+ periods = [str(f.get("period_end")) for f in filings if f.get("period_end")]+ return max(periods) if periods else None+++def _filings_for_period(+ filings: List[Dict[str, Any]], period_end: Optional[str]+) -> List[Dict[str, Any]]:+ """Return every filing a filer made for one quarter, original and amendments alike."""+ if period_end is None:+ return filings[:1]+ return [f for f in filings if f.get("period_end") == period_end]+++def _information_table_urls(cik: str, accession: str) -> Tuple[List[str], Optional[str]]:+ """List candidate information-table document URLs for one filing.++ Args:+ cik: CIK in any form; leading zeros are dropped for the archive path.+ accession: Accession number with or without dashes.++ Candidates are ordered before any of them is fetched: a filename that reads+ like an information table goes first, then the largest documents, then the+ order EDGAR listed them. Ranking matters more than the cap does — the cap+ exists only so a filing stuffed with decoy XML cannot turn one lookup into+ an unbounded probe loop, and cutting the list at document order alone can+ drop the real table when it happens to be listed late.++ Returns:+ ``(urls, note)`` where ``note`` explains an empty list (for example a+ pre-XML text filing or an oversized document).++ Raises:+ requests.RequestException: If the filing index cannot be fetched.+ """+ cik_digits = str(cik).lstrip("0") or "0"+ base = f"{_ARCHIVES}/{cik_digits}/{str(accession).replace('-', '')}"+ items = (_sec_get(f"{base}/index.json").json().get("directory") or {}).get("item") or []++ ranked: List[Tuple[int, int, int, str]] = []+ oversized = False+ ceiling = _max_xml_bytes()+ for order, item in enumerate(items):+ name = str((item or {}).get("name") or "")+ lowered = name.lower()+ if not lowered.endswith(".xml") or lowered == "primary_doc.xml":+ continue+ try:+ size = int(item.get("size") or 0)+ except (TypeError, ValueError):+ size = 0+ if size > ceiling:+ oversized = True+ continue+ looks_like_table = any(hint in lowered for hint in _TABLE_NAME_HINTS)+ ranked.append((0 if looks_like_table else 1, -size, order, f"{base}/{name}"))++ urls = [entry[-1] for entry in sorted(ranked)[:_MAX_TABLE_CANDIDATES]]+ if urls:+ return urls, None+ if oversized:+ return [], f"information table exceeds the {ceiling} byte ceiling ({_ENV_MAX_XML_MB})"+ return [], "filing carries no XML information table (pre-2013 filings are plain text)"+++# --------------------------------------------------------------------------- #+# Information-table parsing+# --------------------------------------------------------------------------- #+++def _local(tag: Any) -> str:+ """Strip an XML namespace from a tag name."""+ return str(tag).rsplit("}", 1)[-1]+++def _parse_information_table(raw: bytes, cusip_filter: Optional[str] = None) -> List[Dict[str, Any]]:+ """Stream one information-table XML into position rows.++ Parsed incrementally and cleared as it goes: a large manager's table runs to+ tens of thousands of rows, and holding the whole tree would cost far more+ memory than the rows themselves. Bytes (not text) are parsed because filers+ do emit non-UTF-8 declarations such as ``windows-1252``.++ Args:+ raw: The raw XML document bytes.+ cusip_filter: When set, only rows with this CUSIP are kept.++ Returns:+ Row dicts with ``issuer``, ``title_of_class``, ``cusip``, ``value_raw``,+ ``shares``, ``share_type``, ``put_call``, ``discretion``,+ ``other_manager`` and ``voting_sole``/``voting_shared``/``voting_none``.++ Raises:+ ValueError: When the document root is not ``informationTable``.+ xml.etree.ElementTree.ParseError: On malformed XML.+ """+ context = ET.iterparse(io.BytesIO(raw), events=("start", "end"))+ _, root = next(context)+ if _local(root.tag) != "informationTable":+ raise ValueError(f"document root is <{_local(root.tag)}>, not <informationTable>")++ wanted = cusip_filter.strip().upper() if cusip_filter else None+ rows: List[Dict[str, Any]] = []+ for event, element in context:+ if event != "end" or _local(element.tag) != "infoTable":+ continue+ fields = {_local(node.tag): (node.text or "").strip() for node in element.iter()}+ element.clear()+ root.clear()+ cusip = fields.get("cusip", "").upper()+ if wanted and cusip != wanted:+ continue+ rows.append(+ {+ "issuer": fields.get("nameOfIssuer") or None,+ "title_of_class": fields.get("titleOfClass") or None,+ "cusip": cusip or None,+ "value_raw": _to_number(fields.get("value")),+ "shares": _to_number(fields.get("sshPrnamt")),+ "share_type": fields.get("sshPrnamtType") or None,+ "put_call": fields.get("putCall") or None,+ "discretion": fields.get("investmentDiscretion") or None,+ "other_manager": fields.get("otherManager") or None,+ "voting_sole": _to_number(fields.get("Sole")),+ "voting_shared": _to_number(fields.get("Shared")),+ "voting_none": _to_number(fields.get("None")),+ }+ )+ if len(rows) >= _MAX_INFOTABLE_ROWS:+ break+ return rows+++def _detect_value_units(rows: List[Dict[str, Any]], filing_date: Optional[str]) -> Tuple[int, str, str]:+ """Decide whether ``value`` is whole dollars or thousands of dollars.++ The filing date sets the prior (whole dollars from 2023-01-03). Because+ filers straddled that boundary in practice, the prior is then checked+ against the median implied price of the share-denominated rows and+ overridden when the two disagree; an equity portfolio priced in dollars+ cannot plausibly have a sub-$1 median share price, and one priced in+ thousands cannot plausibly show a median above $3.++ Args:+ rows: Parsed information-table rows.+ filing_date: The filing's ``YYYY-MM-DD`` date, if known.++ Returns:+ ``(multiplier, units, basis)`` where ``multiplier`` converts ``value_raw``+ to USD, ``units`` is ``"usd"`` or ``"usd_thousands"`` and ``basis`` names+ the evidence used.+ """+ dollars = str(filing_date or "9999-99-99") >= _DOLLAR_UNITS_FROM+ implied = sorted(+ row["value_raw"] / row["shares"]+ for row in rows+ if row.get("share_type") in (None, "SH")+ and row.get("shares")+ and row.get("value_raw") is not None+ and row["shares"] > 0+ )+ if len(implied) >= _MIN_ROWS_FOR_UNIT_CHECK:+ median = implied[len(implied) // 2]+ if dollars and median < _IMPLIED_PRICE_MIN_USD:+ return 1000, "usd_thousands", f"implied_price_override (median {median:.4f} per share)"+ if not dollars and median > _IMPLIED_PRICE_MAX_THOUSANDS:+ return 1, "usd", f"implied_price_override (median {median:.4f} per share)"+ return (1, "usd", "filing_date+implied_price") if dollars else (+ 1000,+ "usd_thousands",+ "filing_date+implied_price",+ )+ return (1, "usd", "filing_date") if dollars else (1000, "usd_thousands", "filing_date")+++def _aggregate(rows: List[Dict[str, Any]], multiplier: int) -> List[Dict[str, Any]]:+ """Collapse duplicate rows for one security into a single position.++ A manager that spreads discretion across sub-advisers files one row per+ sub-adviser for the same CUSIP, so ranking the raw rows would both split and+ understate the real position.++ Args:+ rows: Parsed information-table rows.+ multiplier: Factor converting ``value_raw`` to USD.++ Returns:+ Positions sorted by USD value, largest first.+ """+ merged: Dict[Tuple[Any, ...], Dict[str, Any]] = {}+ for row in rows:+ key = (row["cusip"], row["title_of_class"], row["put_call"], row["share_type"])+ position = merged.get(key)+ if position is None:+ position = {+ "issuer": row["issuer"],+ "cusip": row["cusip"],+ "title_of_class": row["title_of_class"],+ "put_call": row["put_call"],+ "share_type": row["share_type"],+ "value_usd": 0.0,+ "shares": 0.0,+ "rows": 0,+ }+ merged[key] = position+ position["value_usd"] += (row["value_raw"] or 0.0) * multiplier+ position["shares"] += row["shares"] or 0.0+ position["rows"] += 1++ positions = sorted(merged.values(), key=lambda p: p["value_usd"], reverse=True)+ total = sum(p["value_usd"] for p in positions) or 0.0+ for position in positions:+ position["value_usd"] = round(position["value_usd"], 2)+ position["weight_pct"] = round(100.0 * position["value_usd"] / total, 4) if total else None+ return positions+++def _fetch_holdings(cik: str, filing: Dict[str, Any], cusip_filter: Optional[str] = None) -> Dict[str, Any]:+ """Fetch and shape one filing's holdings. Never raises.++ Args:+ cik: Padded CIK of the filer.+ filing: A filing dict from :func:`_list_13f_filings`.+ cusip_filter: Restrict parsing to a single CUSIP.++ Returns:+ The filing metadata plus ``positions`` on success, or an ``error`` key+ describing why this one filing could not be read.+ """+ result = {+ "accession": filing.get("accession"),+ "form": filing.get("form"),+ "filing_date": filing.get("filing_date"),+ "quarter": _quarter_label(filing.get("period_end")),+ "period_end": filing.get("period_end"),+ }+ try:+ urls, note = _information_table_urls(cik, filing["accession"])+ except Exception as exc: # noqa: BLE001 - one unreadable filing must not kill the batch+ return {**result, "error": f"filing index request failed: {exc}"}+ if not urls:+ return {**result, "error": note or "no information table found"}++ last_error: Optional[str] = None+ for url in urls:+ try:+ raw = _sec_get(url).content+ except Exception as exc: # noqa: BLE001 - try the next candidate document+ last_error = f"{url.rsplit('/', 1)[-1]}: {exc}"+ continue+ if len(raw) > _max_xml_bytes():+ last_error = f"{url.rsplit('/', 1)[-1]}: document exceeds the size ceiling"+ continue+ try:+ rows = _parse_information_table(raw, cusip_filter)+ except Exception as exc: # noqa: BLE001 - a non-table XML is expected among candidates+ last_error = f"{url.rsplit('/', 1)[-1]}: {exc}"+ continue+ # Unit detection must see the whole table; a CUSIP-filtered read has too+ # few rows for the median check, so it is re-derived from all rows.+ scale_rows = rows if cusip_filter is None else _parse_information_table(raw)+ multiplier, units, basis = _detect_value_units(scale_rows, filing.get("filing_date"))+ return {+ **result,+ "document_url": url,+ "value_units": units,+ "value_units_basis": basis,+ "row_count": len(rows),+ "truncated_rows": len(rows) >= _MAX_INFOTABLE_ROWS,+ "positions": _aggregate(rows, multiplier),+ }+ return {**result, "error": last_error or "no parseable information table"}+++def _fetch_summary(cik: str, accession: str) -> Dict[str, Any]:+ """Read a filing's ``primary_doc.xml`` cover and summary page. Never raises.++ The cover page is the only place the amendment semantics are recorded, so+ ``isAmendment``, ``amendmentNo`` and ``amendmentInfo/amendmentType`` are read+ verbatim here and interpreted in :func:`_classify_filing`. Each of those tag+ names occurs at most once per document, so the flat first-wins field map is+ safe for them.++ Args:+ cik: CIK in any form.+ accession: Accession number.++ Returns:+ ``{manager, report_type, period_of_report, total_value_raw,+ holdings_count, is_amendment, amendment_no, amendment_type}`` where+ known, or ``{"error": ...}``.+ """+ cik_digits = str(cik).lstrip("0") or "0"+ url = f"{_ARCHIVES}/{cik_digits}/{str(accession).replace('-', '')}/primary_doc.xml"+ try:+ root = ET.fromstring(_sec_get(url).content)+ except Exception as exc: # noqa: BLE001 - one bad cover page must not kill a scan+ return {"error": str(exc)}+ fields: Dict[str, str] = {}+ for node in root.iter():+ name = _local(node.tag)+ text = (node.text or "").strip()+ if text and name not in fields:+ fields[name] = text+ return {+ "manager": fields.get("name"),+ "report_type": fields.get("reportType"),+ "period_of_report": fields.get("reportCalendarOrQuarter"),+ "total_value_raw": _to_number(fields.get("tableValueTotal")),+ "holdings_count": _to_number(fields.get("tableEntryTotal")),+ "is_amendment": _to_bool(fields.get("isAmendment")),+ "amendment_no": _to_number(fields.get("amendmentNo")),+ "amendment_type": (fields.get("amendmentType") or "").strip().upper() or None,+ }+++def _summary_value_usd(summary: Dict[str, Any], filing_date: Optional[str]) -> Optional[float]:+ """Convert a cover page's reported total to USD using the filing-date prior.++ A cover page carries no per-share figures, so the implied-price check that+ guards :func:`_detect_value_units` is unavailable here and the filing-date+ prior is all there is. Callers must present the result as approximate; use+ :func:`_summary_units` when the prior needs checking against evidence.+ """+ total = summary.get("total_value_raw")+ if total is None:+ return None+ return round(total * (1 if str(filing_date or "") >= _DOLLAR_UNITS_FROM else 1000), 2)+++def _summary_units(+ cik: str,+ filing: Dict[str, Any],+ summary: Dict[str, Any],+) -> Tuple[Optional[float], Optional[str], str]:+ """Scale a cover-page total to USD, checking the prior when it looks wrong.++ The filing-date prior alone misreads the roughly 5% of managers who still+ report in thousands (see :data:`_FILING_THRESHOLD_USD`). Whenever the prior+ produces a total below the $100M level that compels filing at all, the+ filing's own information table is read and its implied-price verdict — the+ same evidence :func:`_detect_value_units` uses — decides the unit. Nothing+ is rescaled on the strength of the threshold alone.++ Args:+ cik: Padded CIK of the filer.+ filing: Filing dict carrying ``accession`` and ``filing_date``.+ summary: The parsed cover page from :func:`_fetch_summary`.++ Returns:+ ``(value_usd, units, basis)``; ``value_usd`` is ``None`` when the cover+ page carried no total.+ """+ value = _summary_value_usd(summary, filing.get("filing_date"))+ dollars = str(filing.get("filing_date") or "") >= _DOLLAR_UNITS_FROM+ units = "usd" if dollars else "usd_thousands"+ if value is None or not 0 < value < _FILING_THRESHOLD_USD:+ return value, units, "filing_date"++ detail = _fetch_holdings(cik, filing)+ if "error" in detail:+ return value, units, f"filing_date (unverified: {detail['error']})"+ table_units = detail.get("value_units")+ if table_units == units:+ return value, units, "filing_date+information_table"+ raw = summary.get("total_value_raw") or 0.0+ multiplier = 1000 if table_units == "usd_thousands" else 1+ return round(raw * multiplier, 2), str(table_units), "information_table_implied_price"+++# --------------------------------------------------------------------------- #+# Amendment chain+# --------------------------------------------------------------------------- #+++def _without_positions(detail: Dict[str, Any]) -> Dict[str, Any]:+ """Return a filing detail stripped of its position list, safe to embed in an envelope."""+ return {k: v for k, v in detail.items() if k != "positions"}+++def _classify_filing(filing: Dict[str, Any], summary: Dict[str, Any]) -> Dict[str, Any]:+ """Decide what one filing does to the quarter it belongs to.++ Two independent signals exist and live filings show them disagreeing in both+ directions (see gotcha 3 in the module docstring), so a filing counts as an+ amendment when *either* the submission type ends in ``/A`` or the cover page+ sets ``isAmendment``. Only then does ``amendmentType`` choose between+ replacing the quarter and extending it; a type that is absent or outside the+ two values SEC filers actually use resolves to+ :data:`_ROLE_UNSPECIFIED`, which no caller acts on.++ Args:+ filing: A filing dict carrying at least ``form``.+ summary: The parsed cover page from :func:`_fetch_summary`.++ Returns:+ ``{is_amendment, amendment_no, amendment_type, amendment_type_known,+ role, role_basis}``. ``role_basis`` names every signal that was read so+ a caller can see *why* a filing was classified the way it was.+ """+ form = str(filing.get("form") or "").upper()+ form_says = form.endswith("/A")+ cover_error = summary.get("error")+ cover_says = None if cover_error else summary.get("is_amendment")+ declared = None if cover_error else summary.get("amendment_type")++ is_amendment = bool(cover_says) or form_says+ role = _AMENDMENT_ROLES.get(declared or "", _ROLE_UNSPECIFIED) if is_amendment else _ROLE_ORIGINAL++ signals = [f"submissionType={form or 'unknown'}"]+ if cover_error:+ signals.append(f"coverPage=unreadable ({cover_error})")+ else:+ signals.append(f"coverPage.isAmendment={cover_says}")+ signals.append(f"coverPage.amendmentType={declared or 'absent'}")+ return {+ "is_amendment": is_amendment,+ "amendment_no": None if cover_error else summary.get("amendment_no"),+ "amendment_type": declared,+ "amendment_type_known": (role != _ROLE_UNSPECIFIED) if is_amendment else None,+ "role": role,+ "role_basis": "; ".join(signals),+ }+++def _quarter_chain(+ cik: str, filings: List[Dict[str, Any]], budget: Optional["_Budget"] = None+) -> Tuple[List[Dict[str, Any]], bool]:+ """Read one quarter's filings into an ordered amendment chain.++ Ordering is the order the filings take effect: originals first, then+ amendments oldest-first by filing date with ``amendmentNo`` breaking ties.+ DZ BANK filed both of its 2020-09-30 amendments on 2026-05-04, so the number+ is what puts the ``RESTATEMENT`` (no. 1) ahead of the ``NEW HOLDINGS``+ (no. 2) that extends it.++ Args:+ cik: Padded CIK of the filer.+ filings: Every filing that filer made for the one quarter.+ budget: Optional wall-clock guard; the chain stops short rather than+ overrunning it.++ Returns:+ ``(chain, truncated)``; each entry is the filing dict plus its parsed+ ``summary`` and the verdict from :func:`_classify_filing`.+ """+ truncated = len(filings) > _MAX_CHAIN_FILINGS+ chain: List[Dict[str, Any]] = []+ for filing in filings[:_MAX_CHAIN_FILINGS]:+ if budget is not None and not budget.ok():+ truncated = True+ break+ summary = _fetch_summary(cik, filing["accession"])+ chain.append({**filing, "summary": summary, **_classify_filing(filing, summary)})+ chain.sort(+ key=lambda f: (+ 0 if f["role"] == _ROLE_ORIGINAL else 1,+ str(f.get("filing_date") or ""),+ f["amendment_no"] if f["amendment_no"] is not None else 0.0,+ str(f.get("accession") or ""),+ )+ )+ return chain, truncated+++def _merge_positions(+ base: List[Dict[str, Any]], extra: List[Dict[str, Any]]+) -> Tuple[List[Dict[str, Any]], int]:+ """Union two aggregated position lists, summing anything they both report.++ Both sides are already scaled to USD by :func:`_aggregate`, which is what+ makes this safe across a chain whose filings use different value units — DZ+ BANK's 2020-09-30 original is denominated in thousands and its 2026+ amendments in whole dollars.++ A ``NEW HOLDINGS`` amendment is meant to carry securities the original+ omitted, so overlap should be empty. When it is not, the rows are summed+ (that is what "add new holdings" says) and the overlap is counted so the+ caller can see that the filer reported the same security twice rather than+ having it silently folded in.++ Args:+ base: Positions accumulated so far.+ extra: Positions from the amendment being merged in.++ Returns:+ ``(positions, overlapping_securities)`` sorted by USD value, largest first.+ """+ merged: Dict[Tuple[Any, ...], Dict[str, Any]] = {}+ overlaps = 0+ for source in (base, extra):+ for position in source:+ key = (+ position["cusip"],+ position["title_of_class"],+ position["put_call"],+ position["share_type"],+ )+ slot = merged.get(key)+ if slot is None:+ merged[key] = dict(position)+ continue+ if source is extra:+ overlaps += 1+ slot["value_usd"] += position["value_usd"]+ slot["shares"] += position["shares"]+ slot["rows"] += position["rows"]+ slot["issuer"] = slot["issuer"] or position["issuer"]++ positions = sorted(merged.values(), key=lambda p: p["value_usd"], reverse=True)+ total = sum(p["value_usd"] for p in positions) or 0.0+ for position in positions:+ position["value_usd"] = round(position["value_usd"], 2)+ position["weight_pct"] = round(100.0 * position["value_usd"] / total, 4) if total else None+ return positions, overlaps+++def _unresolved_reason(role: str) -> Optional[str]:+ """Explain why a filing cannot be applied, or ``None`` when it can be.++ Two filings in one quarter cannot both be applied without a rule saying how,+ and only ``amendmentType`` supplies such a rule. A second filing that is not+ an amendment at all supplies none. It is rare but it does happen — none of+ the 9,312 filer-quarters in the 2026-03-01..2026-05-31 Form 13F data set+ produced one, yet 2 of the 8,933 filer-quarters in 2023q2 did — so rather+ than inventing a merge-or-replace convention off two observations, it is+ reported unapplied and the caller is told.+ """+ if role == _ROLE_UNSPECIFIED:+ return (+ "the cover page declares an amendment but names no amendmentType, so whether this "+ "table replaces the quarter or only adds to it is UNKNOWN"+ )+ if role == _ROLE_ORIGINAL:+ return (+ "a second filing for this quarter that is not marked as an amendment, so whether it "+ "supersedes or supplements the first is UNKNOWN"+ )+ return None+++def _resolution_label(applied_roles: List[str], unresolved_amendments: int, unresolved_originals: int) -> str:+ """Name what the chain walk actually did, in one machine-readable token."""+ if applied_roles[:1] == [_ROLE_NEW_HOLDINGS]:+ # Nothing to add to: a NEW HOLDINGS amendment reports only what a base+ # filing omitted, so on its own it is a fragment of a quarter and must+ # not be labelled as though a base had been found and extended.+ return "new_holdings_without_base"+ if unresolved_amendments:+ return "incomplete_unspecified_amendment"+ if unresolved_originals:+ return "incomplete_duplicate_originals"+ if _ROLE_RESTATEMENT in applied_roles and _ROLE_NEW_HOLDINGS in applied_roles:+ return "restated_then_extended"+ if _ROLE_RESTATEMENT in applied_roles:+ return "restated"+ if _ROLE_NEW_HOLDINGS in applied_roles:+ return "original_plus_new_holdings"+ return "original_only"+++def _walk_chain(+ cik: str,+ chain: List[Dict[str, Any]],+ cusip_filter: Optional[str],+ budget: Optional["_Budget"],+ skip_superseded: bool,+) -> Dict[str, Any]:+ """Walk an amendment chain into the quarter's actual holdings. Never raises.++ ``RESTATEMENT`` replaces everything accumulated so far; ``NEW HOLDINGS`` is+ merged on top of it; an amendment whose type the filer left blank is fetched+ and described but **not** applied, because applying it either way would be a+ guess. When such an amendment is the only readable table for the quarter it+ is reported anyway — there is nothing else — and the resolution says so.++ Args:+ cik: Padded CIK of the filer.+ chain: Ordered chain from :func:`_quarter_chain`.+ cusip_filter: Restrict every table read to a single CUSIP.+ budget: Optional wall-clock guard.++ Returns:+ ``{positions, standing_filing, sources, resolution, resolution_note,+ unresolved_amendments, overlapping_positions}`` or ``{"error": ...}``+ when no table in the chain could be read.+ """+ positions: Optional[List[Dict[str, Any]]] = None+ standing: Optional[Dict[str, Any]] = None+ sources: List[Dict[str, Any]] = []+ unresolved: List[Tuple[int, Dict[str, Any]]] = []+ unresolved_originals = 0+ applied_roles: List[str] = []+ overlaps = 0+ last_error: Optional[str] = None++ # A filing that a later RESTATEMENT wipes out contributes nothing, so its+ # table is never downloaded. Skipping it is not just cheaper: DZ BANK's+ # superseded 2020-09-30 original is a 1,851-row document read only to be+ # discarded. If the restatement then turns out to be unreadable the walk is+ # repeated over the whole chain (``superseded`` is cleared), so nothing is+ # lost to the optimization.+ replacements = [i for i, e in enumerate(chain) if e["role"] == _ROLE_RESTATEMENT]+ superseded: set = set()+ if skip_superseded and replacements:+ superseded = {+ i for i in range(replacements[-1]) if chain[i]["role"] in (_ROLE_ORIGINAL, _ROLE_RESTATEMENT)+ }++ for index, entry in enumerate(chain):+ if budget is not None and not budget.ok():+ break+ record = {+ "accession": entry.get("accession"),+ "form": entry.get("form"),+ "filing_date": entry.get("filing_date"),+ "role": entry["role"],+ "amendment_no": entry["amendment_no"],+ "amendment_type": entry["amendment_type"],+ "amendment_type_known": entry["amendment_type_known"],+ "role_basis": entry["role_basis"],+ }+ if index in superseded:+ sources.append(+ {+ **record,+ "applied": False,+ "not_applied_reason": "superseded by a later RESTATEMENT for the same quarter",+ }+ )+ continue+ detail = _fetch_holdings(cik, entry, cusip_filter)+ if "error" in detail:+ last_error = detail["error"]+ sources.append({**record, "applied": False, "error": detail["error"]})+ continue+ record.update(+ row_count=detail["row_count"],+ document_url=detail["document_url"],+ value_units=detail["value_units"],+ value_units_basis=detail["value_units_basis"],+ )++ # An unspecified amendment is never applied. A plain filing is applied+ # only while nothing has been applied yet — a second one has no stated+ # relationship to the first.+ blocked = entry["role"] == _ROLE_UNSPECIFIED or (+ entry["role"] == _ROLE_ORIGINAL and positions is not None+ )+ if blocked:+ sources.append(+ {**record, "applied": False, "not_applied_reason": _unresolved_reason(entry["role"])}+ )+ if entry["role"] == _ROLE_ORIGINAL:+ unresolved_originals += 1+ else:+ unresolved.append((len(sources) - 1, detail))+ continue++ if positions is None or entry["role"] == _ROLE_RESTATEMENT:+ effect = "replace" if entry["role"] == _ROLE_RESTATEMENT else "base"+ positions, standing = detail["positions"], _without_positions(detail)+ sources.append({**record, "applied": True, "effect": effect})+ else:+ positions, hit = _merge_positions(positions, detail["positions"])+ overlaps += hit+ sources.append({**record, "applied": True, "effect": "merge"})+ applied_roles.append(entry["role"])++ if positions is None and unresolved:+ index, detail = unresolved[-1]+ positions, standing = detail["positions"], _without_positions(detail)+ sources[index] = {+ **sources[index],+ "applied": True,+ "effect": "only_readable_table",+ "not_applied_reason": None,+ }+ note = (+ "The only readable table for this quarter is an amendment whose cover page names no "+ "amendmentType. It is reported as-is and is NOT confirmed to be the complete quarter."+ )+ return {+ "positions": positions,+ "standing_filing": standing,+ "sources": sources,+ "resolution": "unverified_amendment_only",+ "resolution_note": note,+ "unresolved_amendments": len(unresolved),+ "unresolved_duplicate_originals": unresolved_originals,+ "overlapping_positions": overlaps,+ }++ if positions is None:+ return {"error": last_error or "no readable information table in this quarter's filings"}++ resolution = _resolution_label(applied_roles, len(unresolved), unresolved_originals)+ notes = {+ "original_only": "One original 13F-HR, no amendment applied.",+ "restated": (+ "A 13F-HR/A of type RESTATEMENT replaced the earlier table for this quarter; the "+ "superseded filing is listed in 'sources' with applied=false."+ ),+ "original_plus_new_holdings": (+ "A 13F-HR/A of type NEW HOLDINGS reports only securities omitted from the base "+ "filing, so its rows were merged on top of it rather than replacing it."+ ),+ "restated_then_extended": (+ "The quarter was restated and then extended: the RESTATEMENT replaced the base table "+ "and the NEW HOLDINGS amendment was merged on top of the restated table."+ ),+ "incomplete_unspecified_amendment": (+ f"{len(unresolved)} amendment(s) for this quarter declare no amendmentType, so it is "+ "UNKNOWN whether they replace these positions or add to them. They were NOT applied; "+ "each is listed in 'sources' with its own row count and document URL so the caller "+ "can decide. Treat these positions as provisional."+ ),+ "incomplete_duplicate_originals": (+ f"{unresolved_originals} further filing(s) for this quarter are not marked as "+ "amendments, so their relationship to the applied filing is UNKNOWN and they were "+ "NOT applied. Treat these positions as provisional."+ ),+ # Whether a base filing is absent or merely unreadable changes what the+ # caller should do about it, so the note must not assert the stronger+ # claim when 'sources' itself lists a base filing that failed to fetch.+ "new_holdings_without_base": (+ "The earliest applicable filing for this quarter is a NEW HOLDINGS amendment, which "+ "reports only securities a base filing omitted — and "+ + (+ "the base filing(s) for this quarter are listed in 'sources' but none could be "+ "read, so none was applied"+ if any(s["role"] in (_ROLE_ORIGINAL, _ROLE_RESTATEMENT) for s in sources)+ else "no base filing was found in this filer's 13F index"+ )+ + ". These positions are therefore a FRAGMENT of the quarter, not the "+ "manager's portfolio."+ ),+ }+ return {+ "positions": positions,+ "standing_filing": standing,+ "sources": sources,+ "resolution": resolution,+ "resolution_note": notes[resolution],+ "unresolved_amendments": len(unresolved),+ "unresolved_duplicate_originals": unresolved_originals,+ "overlapping_positions": overlaps,+ }+++def _resolve_chain_holdings(+ cik: str,+ chain: List[Dict[str, Any]],+ cusip_filter: Optional[str] = None,+ budget: Optional["_Budget"] = None,+) -> Dict[str, Any]:+ """Resolve one quarter's holdings, retrying the superseded filings if needed.++ The fast walk never downloads a table a later ``RESTATEMENT`` replaces. The+ only thing that entitles it to skip those tables is the restatement actually+ being applied, so the walk is repeated over the full chain whenever no+ restatement was — whether it left the quarter unreadable outright or merely+ left a ``NEW HOLDINGS`` fragment standing in for a base filing that was+ never downloaded. Checking only for an outright error is not enough: DZ+ BANK's real 2020-09-30 chain is original + RESTATEMENT + NEW HOLDINGS, so a+ single failed restatement fetch would otherwise return the 2-row amendment+ and leave the 1,851-row original skipped.++ Args:+ cik: Padded CIK of the filer.+ chain: Ordered chain from :func:`_quarter_chain`.+ cusip_filter: Restrict every table read to a single CUSIP.+ budget: Optional wall-clock guard.++ Returns:+ The dict described by :func:`_walk_chain`.+ """+ resolved = _walk_chain(cik, chain, cusip_filter, budget, skip_superseded=True)+ if not any(entry["role"] == _ROLE_RESTATEMENT for entry in chain):+ return resolved+ if any(s.get("applied") and s["role"] == _ROLE_RESTATEMENT for s in resolved.get("sources") or []):+ return resolved+ return _walk_chain(cik, chain, cusip_filter, budget, skip_superseded=False)+++def _resolve_chain_value(+ cik: str, chain: List[Dict[str, Any]]+) -> Tuple[Optional[float], Optional[str], str, Dict[str, Any]]:+ """Total a quarter's reported portfolio value from cover pages alone.++ Cheaper than :func:`_resolve_chain_holdings` — used by ``top_managers``,+ where only the total is ranked — and follows the same rules: a+ ``RESTATEMENT`` cover total replaces, a ``NEW HOLDINGS`` cover total is+ added, an unspecified amendment is skipped and flagged. Every filing's total+ is scaled to USD independently by :func:`_summary_units`, which is required+ when a 2020 original in thousands is restated in 2026 in whole dollars.++ Args:+ cik: Padded CIK of the filer.+ chain: Ordered chain from :func:`_quarter_chain`.++ Returns:+ ``(value_usd, units, basis, amendment_block)``.+ """+ value: Optional[float] = None+ units: Optional[str] = None+ basis = "no_readable_cover_page"+ applied_roles: List[str] = []+ unresolved: List[Dict[str, Any]] = []+ unresolved_originals = 0+ merged = 0+ standing: Optional[Dict[str, Any]] = None++ replacements = [i for i, e in enumerate(chain) if e["role"] == _ROLE_RESTATEMENT]+ superseded = (+ {i for i in range(replacements[-1]) if chain[i]["role"] in (_ROLE_ORIGINAL, _ROLE_RESTATEMENT)}+ if replacements+ else set()+ )++ for index, entry in enumerate(chain):+ summary = entry.get("summary") or {}+ if "error" in summary or index in superseded:+ continue+ entry_value, entry_units, entry_basis = _summary_units(cik, entry, summary)+ blocked = entry["role"] == _ROLE_UNSPECIFIED or (+ entry["role"] == _ROLE_ORIGINAL and value is not None+ )+ if blocked:+ if entry["role"] == _ROLE_ORIGINAL:+ unresolved_originals += 1+ continue+ unresolved.append(+ {+ "accession": entry.get("accession"),+ "form": entry.get("form"),+ "filing_date": entry.get("filing_date"),+ "reported_value_usd": entry_value,+ "holdings_count": summary.get("holdings_count"),+ }+ )+ continue+ if value is None or entry["role"] == _ROLE_RESTATEMENT:+ value, units, basis, standing = entry_value, entry_units, entry_basis, entry+ else:+ value = round((value or 0.0) + (entry_value or 0.0), 2)+ merged += 1+ applied_roles.append(entry["role"])++ if value is None and unresolved:+ # Same rule as the holdings walk: an unverified amendment is still the+ # only number this quarter has, so it is reported and labelled.+ fallback = unresolved[-1]+ value = fallback["reported_value_usd"]+ units, basis = None, "unverified_amendment_only"++ resolution = (+ "unverified_amendment_only"+ if basis == "unverified_amendment_only"+ else _resolution_label(applied_roles, len(unresolved), unresolved_originals)+ )+ if merged:+ basis = f"{basis}+merged_{merged}_new_holdings_amendment"+ return (+ value,+ units,+ basis,+ {+ "resolution": resolution,+ "standing_accession": (standing or {}).get("accession"),+ "filings_applied": len(applied_roles),+ "unresolved_amendments": unresolved,+ "unresolved_duplicate_originals": unresolved_originals,+ },+ )+++def _amendment_digest(resolved: Dict[str, Any]) -> Dict[str, Any]:+ """Compress a chain walk to the few fields a per-row listing can afford."""+ return {+ "resolution": resolved.get("resolution"),+ "filings_applied": sum(1 for s in resolved.get("sources") or [] if s.get("applied")),+ "unresolved_amendments": resolved.get("unresolved_amendments", 0),+ "unresolved_duplicate_originals": resolved.get("unresolved_duplicate_originals", 0),+ "standing_accession": (resolved.get("standing_filing") or {}).get("accession"),+ "standing_form": (resolved.get("standing_filing") or {}).get("form"),+ }+++# --------------------------------------------------------------------------- #+# Quarter-over-quarter changes+# --------------------------------------------------------------------------- #+++def _diff_positions(current: List[Dict[str, Any]], prior: List[Dict[str, Any]]) -> List[Dict[str, Any]]:+ """Classify quarter-over-quarter position changes.++ Keyed on ``(cusip, put_call)`` rather than the class title, because filers+ spell ``titleOfClass`` inconsistently across quarters while the CUSIP is+ stable.++ Args:+ current: Aggregated positions for the newer quarter.+ prior: Aggregated positions for the older quarter.++ Returns:+ Every change, sorted by the absolute USD move, largest first. Unchanged+ positions are omitted. The list is deliberately uncapped so the caller's+ per-action counts describe the whole quarter; trimming for the result+ budget happens afterwards in :func:`_fit_changes`.+ """+ def index(positions: Iterable[Dict[str, Any]]) -> Dict[Tuple[Any, Any], Dict[str, Any]]:+ out: Dict[Tuple[Any, Any], Dict[str, Any]] = {}+ for position in positions:+ key = (position["cusip"], position["put_call"])+ merged = out.setdefault(+ key, {"issuer": position["issuer"], "value_usd": 0.0, "shares": 0.0}+ )+ merged["value_usd"] += position["value_usd"]+ merged["shares"] += position["shares"]+ return out++ now, before = index(current), index(prior)+ changes: List[Dict[str, Any]] = []+ for key in set(now) | set(before):+ cusip, put_call = key+ new_side = now.get(key)+ old_side = before.get(key)+ new_shares = new_side["shares"] if new_side else 0.0+ old_shares = old_side["shares"] if old_side else 0.0+ if new_side is None:+ action = "closed"+ elif old_side is None:+ action = "new"+ elif new_shares > old_shares:+ action = "increased"+ elif new_shares < old_shares:+ action = "reduced"+ else:+ continue+ new_value = new_side["value_usd"] if new_side else 0.0+ old_value = old_side["value_usd"] if old_side else 0.0+ changes.append(+ {+ "action": action,+ "issuer": (new_side or old_side)["issuer"],+ "cusip": cusip,+ "put_call": put_call,+ "shares_before": round(old_shares, 4),+ "shares_after": round(new_shares, 4),+ "shares_change": round(new_shares - old_shares, 4),+ "shares_change_pct": round(100.0 * (new_shares - old_shares) / old_shares, 4)+ if old_shares+ else None,+ "value_usd_before": round(old_value, 2),+ "value_usd_after": round(new_value, 2),+ "value_usd_change": round(new_value - old_value, 2),+ }+ )+ changes.sort(key=lambda c: abs(c["value_usd_change"]), reverse=True)+ return changes+++# --------------------------------------------------------------------------- #+# Small helpers+# --------------------------------------------------------------------------- #+++def _fit_changes(changes: List[Dict[str, Any]]) -> List[Dict[str, Any]]:+ """Trim the change list to the share of the result budget reserved for it.++ ``fit_records`` pages the position list but the changes block rides along+ inside the fixed part of the envelope, so without this a busy quarter+ (Bridgewater's 2026Q1 diff runs to 200 changes and 60,419 characters) would+ blow the whole result past its cap and be cut mid-structure downstream —+ the exact silent truncation the paging helper exists to prevent.++ Args:+ changes: Changes already sorted by descending dollar impact.++ Returns:+ The longest prefix whose serialization fits the reserved share.+ """+ budget = int(TOOL_RESULT_LIMIT * _CHANGES_BUDGET_SHARE)+ kept = changes[:_MAX_CHANGES]+ while len(kept) > 1:+ size = len(json.dumps(kept, ensure_ascii=False))+ if size <= budget:+ break+ # Shrink proportionally to the overflow so a wide record set converges+ # in a couple of rounds rather than one element at a time.+ kept = kept[: max(1, min(len(kept) - 1, int(len(kept) * budget / size)))]+ return kept+++def _to_number(value: Any) -> Optional[float]:+ """Coerce a text field to ``float``, or ``None`` when absent/non-numeric."""+ if value is None or value == "":+ return None+ try:+ return float(str(value).replace(",", ""))+ except (TypeError, ValueError):+ return None+++def _to_bool(value: Any) -> Optional[bool]:+ """Coerce a cover-page flag to ``bool``, or ``None`` when absent/unreadable.++ ``None`` is a third state, not a false: a cover page that never wrote+ ``isAmendment`` says nothing about whether the filing amends anything, and+ collapsing that to ``False`` is exactly the assumption this module refuses+ to make.+ """+ if value is None:+ return None+ text = str(value).strip().upper()+ if text in _TRUE_TEXT:+ return True+ if text in _FALSE_TEXT:+ return False+ return None+++def _at(seq: Any, idx: int) -> Optional[str]:+ """Return ``seq[idx]`` as a stripped string, or ``None`` when out of range/empty."""+ if not isinstance(seq, list) or idx >= len(seq):+ return None+ value = seq[idx]+ return (str(value).strip() or None) if value is not None else None+++def _clamp(value: Any, default: int, maximum: int) -> int:+ """Coerce a count into ``1..maximum``, falling back to ``default``."""+ try:+ return max(1, min(int(value), maximum))+ except (TypeError, ValueError, OverflowError):+ return default+++def _error(message: str, **extra: Any) -> str:+ """Render a failure envelope as a JSON string."""+ return json.dumps({"ok": False, "error": message, **extra}, ensure_ascii=False)+++def _envelope(mode: str, data: Dict[str, Any], records: List[Any], record_key: str, offset: int) -> str:+ """Serialize an envelope whose ``record_key`` list is paged to fit one result."""++ def build(page: List[Any], paging: Dict[str, Any]) -> Dict[str, Any]:+ return {+ "ok": True,+ "market": "US",+ "source": "sec_edgar_13f",+ "mode": mode,+ "paging": paging,+ "data": {**data, record_key: page},+ }++ return fit_records(records, offset, build)+++# --------------------------------------------------------------------------- #+# Tool+# --------------------------------------------------------------------------- #+++class InstitutionalHoldingsTool(BaseTool):+ """Query SEC Form 13F-HR institutional holdings three ways."""++ name = "get_institutional_holdings"+ description = (+ "U.S. institutional equity holdings from SEC Form 13F-HR (free EDGAR "+ "data, quarterly, filed within 45 days of quarter end). Three modes: "+ "mode='manager_holdings' returns one manager's portfolio for a quarter, "+ "largest position first, optionally with quarter-over-quarter changes "+ "(new / increased / reduced / closed); mode='ticker_holders' returns "+ "which managers reported a given stock; mode='top_managers' ranks "+ "managers by reported 13F portfolio value. Position values are "+ "normalized to USD and each result states which unit the filing used. "+ "Amendments are followed rather than ignored: a quarter is resolved "+ "across every filing the manager made for it, where a 13F-HR/A of type "+ "RESTATEMENT replaces the earlier table and one of type NEW HOLDINGS is "+ "merged on top of it, and 'amendment_resolution' reports which filing "+ "each answer stands on. "+ "LIMITS: 13F covers only U.S.-listed long equity, options and "+ "convertibles reported by managers over $100M — no shorts, no cash, no "+ "non-U.S. listings, and the data is 45+ days stale by construction. "+ "ticker_holders needs a ticker->CUSIP match in the SEC fails-to-deliver "+ "file (broad but not universal); pass 'cusip' directly when a ticker "+ "cannot be resolved. top_managers ranks within a bounded scanned sample, "+ "not the full filer universe. A minority of amendments name no "+ "amendmentType at all; those are NOT applied and the result is flagged "+ "'incomplete_unspecified_amendment' rather than guessed. Example: "+ '{"mode": "manager_holdings", "manager": "Berkshire Hathaway", '+ '"quarter": "2026Q1", "include_changes": true}.'+ )+ parameters = {+ "type": "object",+ "properties": {+ "mode": {+ "type": "string",+ "enum": list(_MODES),+ "description": (+ "'manager_holdings' = one manager's reported portfolio; "+ "'ticker_holders' = which managers hold one security; "+ "'top_managers' = managers ranked by reported portfolio value."+ ),+ "default": "manager_holdings",+ },+ "manager": {+ "type": "string",+ "description": (+ "Required for mode='manager_holdings'. A 13F filer name "+ "(e.g. 'Berkshire Hathaway', 'Bridgewater Associates') or a "+ "CIK (e.g. '0001067983'). Ambiguous names return the best "+ "match plus the other candidates."+ ),+ },+ "ticker": {+ "type": "string",+ "description": (+ "For mode='ticker_holders': the U.S. ticker to look up "+ "(e.g. 'AAPL'). Resolved to a CUSIP via SEC fails-to-deliver "+ "data; supply 'cusip' instead when the symbol is not covered."+ ),+ },+ "cusip": {+ "type": "string",+ "description": (+ "For mode='ticker_holders': the 9-character CUSIP to search "+ "for directly. Takes precedence over 'ticker'."+ ),+ },+ "quarter": {+ "type": "string",+ "description": (+ "Calendar quarter as '2026Q1' or as the quarter-end date "+ "'2026-03-31'. Defaults to the most recent quarter whose "+ "45-day filing deadline has passed."+ ),+ },+ "include_changes": {+ "type": "boolean",+ "description": (+ "For mode='manager_holdings': also diff this quarter against "+ "the manager's previous 13F, classifying every position as "+ "new / increased / reduced / closed. Costs one extra filing "+ "fetch."+ ),+ "default": False,+ },+ "top_n": {+ "type": "integer",+ "description": (+ f"Maximum records to rank and return (1-{_MAX_TOP_N}). "+ f"Defaults to {_DEFAULT_TOP_N} for manager_holdings, "+ f"{_DEFAULT_HOLDER_SAMPLE} for ticker_holders and 20 for "+ "top_managers. For ticker_holders this is also the number of "+ "filings actually downloaded, so it is capped at "+ f"{_MAX_HOLDER_SAMPLE}."+ ),+ },+ "scan_limit": {+ "type": "integer",+ "description": (+ f"For mode='top_managers': how many filings to read before "+ f"ranking (1-{_MAX_SCAN}, default {_DEFAULT_SCAN}). The "+ "ranking is only valid within this scanned sample."+ ),+ },+ "offset": {+ "type": "integer",+ "description": (+ "Index of the first record to return. Records are returned "+ "whole and only as many as fit one result — read "+ "paging.next_offset and call again to continue."+ ),+ },+ },+ "required": [],+ }++ def execute(self, **kwargs: Any) -> str:+ """Dispatch to the requested 13F mode and return a JSON envelope.++ Args:+ **kwargs: ``mode`` (default ``"manager_holdings"``) plus that mode's+ arguments — see :attr:`parameters`.++ Returns:+ A JSON string. On success ``{"ok": true, "market": "US", "source":+ "sec_edgar_13f", "mode": ..., "paging": {...}, "data": {...}}``; on+ failure ``{"ok": false, "error": ...}``.+ """+ mode = kwargs.get("mode", "manager_holdings")+ if not isinstance(mode, str) or mode not in _MODES:+ return _error(f"mode must be one of {list(_MODES)}")++ try:+ offset = max(int(kwargs.get("offset") or 0), 0)+ except (TypeError, ValueError):+ return _error("offset must be an integer")++ quarter_arg = kwargs.get("quarter")+ period_end: Optional[str] = None+ if quarter_arg not in (None, ""):+ try:+ period_end = _parse_quarter(quarter_arg)+ except ValueError as exc:+ return _error(str(exc))++ handler: Callable[..., str] = {+ "manager_holdings": self._manager_holdings,+ "ticker_holders": self._ticker_holders,+ "top_managers": self._top_managers,+ }[mode]+ try:+ return handler(kwargs, period_end, offset)+ except ValueError as exc:+ return _error(str(exc))+ except Exception as exc: # noqa: BLE001 - every failure leaves as an envelope+ logger.exception("get_institutional_holdings failed in mode %s", mode)+ return _error(f"SEC 13F request failed: {exc}")++ # -- modes ------------------------------------------------------------- #++ def _manager_holdings(self, kwargs: Dict[str, Any], period_end: Optional[str], offset: int) -> str:+ """Return one manager's reported portfolio, optionally with QoQ changes."""+ manager = kwargs.get("manager")+ if not isinstance(manager, str) or not manager.strip():+ return _error("mode='manager_holdings' requires 'manager' (a filer name or CIK)")++ cik, name, candidates = _resolve_manager(manager)+ filings = _list_13f_filings(cik)+ if not filings:+ return _error(f"no 13F-HR filings found for CIK {cik}", cik=cik, manager=name)++ target = _target_period(filings, period_end)+ pool = _filings_for_period(filings, target)+ if not pool:+ # Only the most recent filings are indexed here, so an unmatched+ # quarter means "not in the window searched", which is not the same+ # claim as "never filed".+ return _error(+ f"quarter ending {period_end} is not among the {len(filings)} most recent "+ f"13F-HR filings of CIK {cik} (searched back to "+ f"{filings[-1].get('period_end')})",+ cik=cik,+ manager=name,+ available_quarters=[f.get("period_end") for f in filings[:12]],+ )++ budget = _Budget()+ chain, chain_truncated = _quarter_chain(cik, pool, budget)+ resolved = _resolve_chain_holdings(cik, chain, budget=budget)+ if "error" in resolved:+ return _error(+ f"could not read information table: {resolved['error']}",+ cik=cik,+ manager=name,+ quarter=_quarter_label(target),+ filings_tried=[+ {"accession": f.get("accession"), "form": f.get("form"), "role": f["role"]}+ for f in chain+ ],+ )++ top_n = _clamp(kwargs.get("top_n", _DEFAULT_TOP_N), _DEFAULT_TOP_N, _MAX_TOP_N)+ positions = resolved["positions"]+ total_value = round(sum(p["value_usd"] for p in positions), 2)++ data: Dict[str, Any] = {+ "cik": cik,+ "manager": name,+ # ``filing`` is the document whose table currently stands for the+ # quarter, which is the restatement rather than the original once a+ # quarter has been restated.+ "filing": resolved["standing_filing"],+ "amendment_resolution": {+ "resolution": resolved["resolution"],+ "note": resolved["resolution_note"],+ "unresolved_amendments": resolved["unresolved_amendments"],+ "unresolved_duplicate_originals": resolved["unresolved_duplicate_originals"],+ "overlapping_positions": resolved["overlapping_positions"],+ "chain_truncated": chain_truncated,+ "filings": resolved["sources"],+ },+ "position_count": len(positions),+ # ``paging`` describes the ranked slice, so on its own a complete+ # page of a top_n-capped list reads as the whole portfolio. The+ # smallest positions are the ones dropped; say so rather than+ # leaving it to be inferred from position_count.+ "positions_truncated": len(positions) > top_n,+ "positions_ranked_limit": top_n,+ "portfolio_value_usd": total_value,+ }+ if candidates:+ data["other_name_matches"] = candidates++ if kwargs.get("include_changes"):+ data["changes"] = self._changes_block(cik, filings, target, positions, budget)++ return _envelope("manager_holdings", data, positions[:top_n], "positions", offset)++ def _changes_block(+ self,+ cik: str,+ filings: List[Dict[str, Any]],+ period_end: Optional[str],+ positions: List[Dict[str, Any]],+ budget: "_Budget",+ ) -> Dict[str, Any]:+ """Diff a resolved quarter against the manager's next-older quarter.++ The prior quarter is resolved through the same amendment chain as the+ current one — a diff against a superseded original would invent position+ changes the manager never made.+ """+ older = [+ str(f.get("period_end"))+ for f in filings+ if f.get("period_end") and str(f["period_end"]) < str(period_end or "")+ ]+ if not older:+ return {"error": "no earlier 13F-HR filing available to compare against"}+ prior_period = max(older)+ prior_chain, _ = _quarter_chain(cik, _filings_for_period(filings, prior_period), budget)+ prior = _resolve_chain_holdings(cik, prior_chain, budget=budget)+ if "error" in prior:+ return {+ "error": f"prior quarter unreadable: {prior['error']}",+ "prior_quarter": _quarter_label(prior_period),+ }+ changes = _diff_positions(positions, prior["positions"])+ counts: Dict[str, int] = {}+ for change in changes:+ counts[change["action"]] = counts.get(change["action"], 0) + 1+ # ``counts`` is derived from every change, so the per-action totals stay+ # correct even when the item list below is cut to fit.+ kept = _fit_changes(changes)+ standing = prior["standing_filing"] or {}+ return {+ "prior_quarter": _quarter_label(prior_period),+ "prior_period_end": prior_period,+ "prior_accession": standing.get("accession"),+ "prior_amendment_resolution": _amendment_digest(prior),+ "counts": counts,+ "total": len(changes),+ "returned": len(kept),+ "truncated": len(kept) < len(changes),+ "items": kept,+ }++ def _ticker_holders(self, kwargs: Dict[str, Any], period_end: Optional[str], offset: int) -> str:+ """Return the managers that reported a given security in a quarter."""+ cusip = kwargs.get("cusip")+ ticker = kwargs.get("ticker")+ resolution: Dict[str, Any] = {}++ if isinstance(cusip, str) and cusip.strip():+ cusip = cusip.strip().upper()+ resolution = {"cusip_source": "caller"}+ elif isinstance(ticker, str) and ticker.strip():+ try:+ cusip, description, sources = _cusip_for_ticker(ticker)+ except Exception as exc: # noqa: BLE001 - report the mapping failure, don't guess+ return _error(+ f"ticker->CUSIP resolution failed: {exc}. Pass 'cusip' directly to continue.",+ ticker=ticker.strip().upper(),+ )+ if not cusip:+ return _error(+ f"ticker {ticker.strip().upper()!r} is not in the SEC fails-to-deliver file, "+ "which only covers securities that had a settlement failure in the period. "+ "No CUSIP was guessed; pass 'cusip' directly.",+ ticker=ticker.strip().upper(),+ cusip_sources=sources,+ )+ resolution = {+ "cusip_source": "sec_fails_to_deliver",+ "cusip_sources": sources,+ "cusip_description": description,+ "ticker": ticker.strip().upper(),+ }+ else:+ return _error("mode='ticker_holders' requires 'ticker' or 'cusip'")++ target = period_end or _latest_reportable_quarter()+ sample_size = _clamp(+ kwargs.get("top_n", _DEFAULT_HOLDER_SAMPLE), _DEFAULT_HOLDER_SAMPLE, _MAX_HOLDER_SAMPLE+ )+ budget = _Budget()++ # Full-text search hits are documents, not filers: one filing contributes+ # both its cover page and its information table, and a hit may be an+ # amendment for an older quarter. Pages are read only until enough+ # distinct filers for the requested sample have accumulated.+ seen: Dict[str, Dict[str, Any]] = {}+ # Every page of one window repeats that window's total, so the totals are+ # taken per window and only then summed — the windows are disjoint date+ # ranges, so their sum is the document count for the whole span.+ window_totals: List[int] = []+ universe_is_floor = False+ for start, end in _discovery_windows(target):+ cursor = 0+ window_total = 0+ while len(seen) < sample_size and cursor <= _FTS_MAX_FROM and budget.ok():+ page = _fts(+ {"q": f'"{cusip}"', "forms": "13F-HR", "startdt": start, "enddt": end, "from": cursor}+ )+ window_total = max(window_total, page["total"])+ universe_is_floor = universe_is_floor or page["total_is_lower_bound"]+ if not page["hits"]:+ break+ for hit in page["hits"]:+ if hit.get("file_type") != "INFORMATION TABLE" or hit.get("period_ending") != target:+ continue+ cik = (hit.get("ciks") or [None])[0]+ if not cik or cik in seen or not hit.get("adsh"):+ continue+ seen[cik] = {+ "cik": cik,+ "manager": (hit.get("display_names") or [None])[0],+ "accession": hit.get("adsh"),+ "form": hit.get("form"),+ "filing_date": hit.get("file_date"),+ "period_end": hit.get("period_ending"),+ "is_amendment": str(hit.get("form") or "").upper().endswith("/A"),+ }+ cursor += _FTS_PAGE+ window_totals.append(window_total)+ if len(seen) >= sample_size or not budget.ok():+ break+ universe_documents = sum(window_totals)++ holders: List[Dict[str, Any]] = []+ for discovered in list(seen.values())[:sample_size]:+ if not budget.ok():+ break+ holders.append(self._holder_row(discovered, cusip, target, budget))+ # A filer whose restatement dropped the security is reported as not+ # holding it rather than as an error, and both sort below real holders.+ holders.sort(key=lambda h: (h["value_usd"] is None, -(h["value_usd"] or 0.0)))++ data = {+ "cusip": cusip,+ **resolution,+ "quarter": _quarter_label(target),+ "period_end": target,+ "filers_seen": len(seen),+ "matching_documents_total": universe_documents,+ "matching_documents_total_is_lower_bound": universe_is_floor,+ "sampled": len(holders),+ "budget_exhausted": budget.exhausted,+ "ranking_note": (+ "Ranked by position value WITHIN the sampled filers only. The sample is the "+ "first filers EDGAR full-text search returned for this CUSIP, NOT the largest "+ f"holders. 'matching_documents_total' ({universe_documents}"+ f"{'+' if universe_is_floor else ''}) counts matching EDGAR documents in the "+ "filing windows, not filers, and only bounds how many managers reported this "+ "security. Reading every one of them is out of scope for a single call. Each "+ "holder's position is resolved through that filer's full amendment chain for "+ "the quarter, so a filer that appears here because its ORIGINAL filing named "+ "this CUSIP but whose later RESTATEMENT dropped it is reported with held=false."+ ),+ }+ return _envelope("ticker_holders", data, holders, "holders", offset)++ def _holder_row(+ self, discovered: Dict[str, Any], cusip: str, target: str, budget: "_Budget"+ ) -> Dict[str, Any]:+ """Resolve one discovered filer's position through its whole amendment chain.++ Discovery finds a *document*; the answer needs the *quarter*. Reading+ only the document that matched would report a holding a later+ RESTATEMENT removed, or report a ``NEW HOLDINGS`` fragment as if it were+ the filer's whole position.+ """+ cik = discovered["cik"]+ row: Dict[str, Any] = {+ "cik": cik,+ "manager": discovered["manager"],+ "accession": discovered["accession"],+ "form": discovered["form"],+ "filing_date": discovered["filing_date"],+ }+ try:+ pool = _filings_for_period(_list_13f_filings(cik), target) or [discovered]+ scope = "filer_index"+ except Exception as exc: # noqa: BLE001 - fall back to the matched document alone+ pool, scope = [discovered], f"discovered_document_only ({exc})"++ chain, _ = _quarter_chain(cik, pool, budget)+ resolved = _resolve_chain_holdings(cik, chain, cusip_filter=cusip, budget=budget)+ if "error" in resolved:+ return {**row, "value_usd": None, "shares": None, "held": None, "error": resolved["error"]}++ standing = resolved["standing_filing"] or {}+ position = (resolved["positions"] or [None])[0]+ return {+ **row,+ "standing_accession": standing.get("accession"),+ "standing_form": standing.get("form"),+ "held": position is not None,+ "value_usd": position["value_usd"] if position else None,+ "shares": position["shares"] if position else None,+ "put_call": position["put_call"] if position else None,+ "value_units": standing.get("value_units"),+ "amendment_resolution": _amendment_digest(resolved),+ "amendment_scope": scope,+ "error": None,+ }++ def _top_managers(self, kwargs: Dict[str, Any], period_end: Optional[str], offset: int) -> str:+ """Rank 13F filers by reported portfolio value within a bounded sample."""+ target = period_end or _latest_reportable_quarter()+ scan_limit = _clamp(kwargs.get("scan_limit", _DEFAULT_SCAN), _DEFAULT_SCAN, _MAX_SCAN)+ top_n = _clamp(kwargs.get("top_n", 20), 20, _MAX_TOP_N)++ budget = _Budget()+ candidates: Dict[str, Dict[str, Any]] = {}+ # Distinct on-target accessions per filer. Two or more means the quarter+ # was amended inside the windows, so the chain has to be resolved even if+ # the matched document looks plain. Counting accessions rather than hits+ # matters: one filing shows up as several full-text-search documents.+ filings_seen: Dict[str, set] = {}+ # See ticker_holders: one window's total repeats on every page of it, so+ # the per-window totals are summed rather than the per-page ones.+ window_totals: List[int] = []+ universe_is_floor = False+ for start, end in _discovery_windows(target):+ cursor = 0+ window_total = 0+ while len(candidates) < scan_limit and cursor <= _FTS_MAX_FROM and budget.ok():+ page = _fts({"forms": "13F-HR", "startdt": start, "enddt": end, "from": cursor})+ window_total = max(window_total, page["total"])+ universe_is_floor = universe_is_floor or page["total_is_lower_bound"]+ if not page["hits"]:+ break+ for hit in page["hits"]:+ cik = (hit.get("ciks") or [None])[0]+ if not cik or not hit.get("adsh") or hit.get("period_ending") != target:+ continue+ filings_seen.setdefault(cik, set()).add(hit["adsh"])+ if cik in candidates or len(candidates) >= scan_limit:+ continue+ candidates[cik] = {+ "cik": cik,+ "manager": (hit.get("display_names") or [None])[0],+ "accession": hit.get("adsh"),+ "form": hit.get("form"),+ "filing_date": hit.get("file_date"),+ "period_end": hit.get("period_ending"),+ }+ cursor += _FTS_PAGE+ window_totals.append(window_total)+ if len(candidates) >= scan_limit or not budget.ok():+ break+ universe_total = sum(window_totals)++ managers: List[Dict[str, Any]] = []+ for candidate in candidates.values():+ if not budget.ok():+ break+ managers.append(self._manager_row(candidate, target, filings_seen, budget))+ managers.sort(+ key=lambda m: (m["portfolio_value_usd"] is None, -(m["portfolio_value_usd"] or 0.0))+ )++ data = {+ "quarter": _quarter_label(target),+ "period_end": target,+ "scanned": len(managers),+ "scan_limit": scan_limit,+ # Same trap as manager_holdings: more filers can be scanned than are+ # ranked back, and a full page would otherwise look exhaustive.+ "managers_truncated": len(managers) > top_n,+ "managers_ranked_limit": top_n,+ "universe_documents": universe_total,+ "universe_documents_is_lower_bound": universe_is_floor,+ "budget_exhausted": budget.exhausted,+ "ranking_note": (+ "Ranked by the portfolio value each filer reports on its own 13F cover page, "+ "WITHIN the scanned sample only — the sample is what EDGAR full-text search "+ "returned first for the quarter, not the largest filers, so this is a "+ "discovery aid and NOT a league table of the largest managers. Cover-page "+ "totals carry no per-share figures, so their USD unit starts from the filing "+ "date; when that lands below the $100M level that compels filing at all, the "+ "information table is read and its implied-price verdict decides. Each row "+ "reports the unit it used in 'value_units_basis'. Amendments are followed: a "+ "RESTATEMENT's total replaces the original's and a NEW HOLDINGS total is added "+ "to it, so a filer is never ranked on a fragment. 'amendment_scope' says "+ "whether that check ran against the filer's whole 13F index or only against "+ "the documents the discovery windows returned. Raising scan_limit widens the "+ "sample but each extra filing is a separate SEC request; when the time budget "+ "runs out 'budget_exhausted' is true and the sample is short."+ ),+ }+ return _envelope("top_managers", data, managers[:top_n], "managers", offset)++ def _manager_row(+ self,+ candidate: Dict[str, Any],+ target: str,+ filings_seen: Dict[str, set],+ budget: "_Budget",+ ) -> Dict[str, Any]:+ """Value one discovered filer, following its amendment chain when there is one.++ The filer's full 13F index is only pulled when this quarter is actually+ implicated in an amendment — the matched document says so, or discovery+ saw more than one filing from that filer for the quarter. Amendments run+ at roughly 4% of filings, so the common filer still costs exactly one+ cover-page request and a wide scan does not double in length.+ """+ cik = candidate["cik"]+ summary = _fetch_summary(cik, candidate["accession"])+ verdict = _classify_filing(candidate, summary)+ chain = [{**candidate, "summary": summary, **verdict}]+ scope = "discovery_window"++ if verdict["is_amendment"] or len(filings_seen.get(cik) or ()) > 1:+ try:+ indexed = _filings_for_period(_list_13f_filings(cik), target)+ except Exception as exc: # noqa: BLE001 - keep the discovered document+ indexed, scope = [], f"discovery_window (filer index unavailable: {exc})"+ if indexed:+ chain, _ = _quarter_chain(cik, indexed, budget)+ scope = "filer_index"++ value, units, basis, amendment = _resolve_chain_value(cik, chain)+ standing = next((f for f in chain if f.get("accession") == amendment["standing_accession"]), None)+ return {+ **candidate,+ "manager": summary.get("manager") or candidate["manager"],+ "report_type": summary.get("report_type"),+ "holdings_count": ((standing or {}).get("summary") or summary).get("holdings_count"),+ "portfolio_value_usd": value,+ "value_units": units,+ "value_units_basis": basis,+ "amendment_resolution": amendment,+ "amendment_scope": scope,+ "error": summary.get("error"),+ }diff --git a/agent/tests/test_institutional_holdings_tool.py b/agent/tests/test_institutional_holdings_tool.pynew file mode 100644index 000000000..cf98cc9bf--- /dev/null+++ b/agent/tests/test_institutional_holdings_tool.py@@ -0,0 +1,1739 @@+"""Tests for get_institutional_holdings: 13F parsing, unit detection, envelopes.++Every SEC round trip is mocked at :func:`institutional_holdings_tool._sec_get`,+the single choke point through which the tool reaches the network, so no test+touches ``sec.gov`` or ``efts.sec.gov``. The XML fixtures are trimmed copies of+real information tables, including the duplicate-CUSIP layout a manager with+sub-advisers actually files.+"""++from __future__ import annotations++import io+import json+import zipfile+from datetime import date, timedelta+from typing import Any, Dict, List+from unittest.mock import patch+from xml.sax.saxutils import escape++import pytest++from src.config.limits import TOOL_RESULT_LIMIT+from src.tools import institutional_holdings_tool as iht+from src.tools.institutional_holdings_tool import InstitutionalHoldingsTool++_NS = 'xmlns="http://www.sec.gov/edgar/document/thirteenf/informationtable"'++[email protected](autouse=True)+def _no_network_through_the_submissions_fallback():+ """Fail loudly if a test reaches the live submissions endpoint.++ ``_list_13f_filings`` falls back to :func:`get_submissions` whenever+ full-text search fails, and that helper carries its own HTTP client — so+ patching ``_sec_get`` alone does not stop a test from calling sec.gov.+ Tests that exercise the fallback patch it themselves and override this.+ """+ with patch.object(iht, "get_submissions", side_effect=AssertionError("live SEC submissions call")):+ yield+++def _info_table(rows: List[Dict[str, Any]], declaration: str = '<?xml version="1.0"?>') -> bytes:+ """Render an information-table XML document from row dicts.++ Issuer names are XML-escaped because real filings carry ampersands+ (``National Fire & Marine Insurance Co``).+ """+ body = "".join(+ f"""<infoTable>+ <nameOfIssuer>{escape(r['issuer'])}</nameOfIssuer>+ <titleOfClass>{r.get('cls', 'COM')}</titleOfClass>+ <cusip>{r['cusip']}</cusip>+ <value>{r['value']}</value>+ <shrsOrPrnAmt>+ <sshPrnamt>{r['shares']}</sshPrnamt>+ <sshPrnamtType>{r.get('type', 'SH')}</sshPrnamtType>+ </shrsOrPrnAmt>+ {'<putCall>' + r['put_call'] + '</putCall>' if r.get('put_call') else ''}+ <investmentDiscretion>SOLE</investmentDiscretion>+ <votingAuthority><Sole>{r['shares']}</Sole><Shared>0</Shared><None>0</None></votingAuthority>+ </infoTable>"""+ for r in rows+ )+ return f"{declaration}<informationTable {_NS}>{body}</informationTable>".encode()+++# Berkshire's real table repeats one CUSIP once per sub-adviser; these two AAPL+# rows must aggregate into a single 300-share position, not two.+_CURRENT_ROWS = [+ {"issuer": "APPLE INC", "cusip": "037833100", "value": 25000, "shares": 100},+ {"issuer": "APPLE INC", "cusip": "037833100", "value": 50000, "shares": 200},+ {"issuer": "COCA COLA CO", "cusip": "191216100", "value": 12000, "shares": 200},+ {"issuer": "VISA INC", "cusip": "92826C839", "value": 9000, "shares": 30},+ {"issuer": "NVIDIA CORP", "cusip": "67066G104", "value": 4000, "shares": 25},+ {"issuer": "SPDR S&P 500", "cusip": "78462F103", "value": 2000, "shares": 4},+]+_PRIOR_ROWS = [+ {"issuer": "APPLE INC", "cusip": "037833100", "value": 20000, "shares": 400},+ {"issuer": "COCA COLA CO", "cusip": "191216100", "value": 12000, "shares": 200},+ {"issuer": "TESLA INC", "cusip": "88160R101", "value": 7000, "shares": 40},+ {"issuer": "VISA INC", "cusip": "92826C839", "value": 8000, "shares": 30},+ {"issuer": "NVIDIA CORP", "cusip": "67066G104", "value": 3000, "shares": 25},+ {"issuer": "SPDR S&P 500", "cusip": "78462F103", "value": 1000, "shares": 4},+]++_INDEX_JSON = {+ "directory": {+ "item": [+ {"name": "0001193125-26-226661-index.html", "size": ""},+ {"name": "primary_doc.xml", "size": "5555"},+ {"name": "53405.xml", "size": "45259"},+ ]+ }+}++_PRIMARY_DOC = b"""<?xml version="1.0"?>+<edgarSubmission xmlns="http://www.sec.gov/edgar/thirteenffiler">+ <formData><coverPage>+ <reportCalendarOrQuarter>03-31-2026</reportCalendarOrQuarter>+ <filingManager><name>Test Capital LLC</name></filingManager>+ <reportType>13F HOLDINGS REPORT</reportType>+ </coverPage>+ <summaryPage><tableEntryTotal>6</tableEntryTotal><tableValueTotal>102000</tableValueTotal></summaryPage>+ </formData>+</edgarSubmission>"""+++def _amended_cover(amendment_type: str | None, number: int = 1, total: int = 102000) -> bytes:+ """Render a cover page that declares an amendment, exactly as EDGAR nests it.++ Copied from DZ BANK's live 2020-09-30 amendments: ``isAmendment`` and+ ``amendmentNo`` sit directly under ``coverPage`` while ``amendmentType`` is+ wrapped in ``amendmentInfo``. Passing ``None`` reproduces the AEGON ASSET+ MANAGEMENT UK filings, which are submitted as 13F-HR/A yet declare no+ amendment fields at all.+ """+ block = f"<isAmendment>true</isAmendment><amendmentNo>{number}</amendmentNo>"+ if amendment_type is not None:+ block += f"<amendmentInfo><amendmentType>{amendment_type}</amendmentType></amendmentInfo>"+ doc = _PRIMARY_DOC.replace(b"<reportType>", block.encode() + b"<reportType>")+ return doc.replace(b"<tableValueTotal>102000<", f"<tableValueTotal>{total}<".encode())+++def _untyped_amendment_cover(total: int = 102000) -> bytes:+ """A cover page with no amendment markers at all (the AEGON shape)."""+ return _PRIMARY_DOC.replace(b"<tableValueTotal>102000<", f"<tableValueTotal>{total}<".encode())+++class _Response:+ """Minimal stand-in for the ``requests.Response`` fields the tool reads."""++ def __init__(self, *, content: bytes = b"", payload: Any = None, text: str = "") -> None:+ self.content = content+ self.text = text+ self._payload = payload++ def json(self) -> Any:+ if self._payload is None:+ raise ValueError("no JSON payload configured")+ return self._payload+++def _fts_hit(**overrides: Any) -> Dict[str, Any]:+ """Build one EDGAR full-text search hit in the live ``_source`` shape."""+ source = {+ "ciks": ["0001067983"],+ "display_names": ["TEST CAPITAL LLC (CIK 0001067983)"],+ "form": "13F-HR",+ "file_date": "2026-05-15",+ "period_ending": "2026-03-31",+ "adsh": "0001193125-26-226661",+ "file_type": "13F-HR",+ }+ source.update(overrides)+ return {"_id": f"{source['adsh']}:primary_doc.xml", "_source": source}+++def _fts_payload(hits: List[Dict[str, Any]], total: int | None = None, relation: str = "eq") -> Dict[str, Any]:+ """Wrap hits in the full-text-search envelope shape."""+ return {"hits": {"total": {"value": total if total is not None else len(hits), "relation": relation}, "hits": hits}}+++# A real 13F filer is above the $100M threshold that compels filing, so a+# cover page used for ranking has to sit above it too — below that the tool+# stops trusting the filing-date unit prior and goes and reads the information+# table, which is a different code path from the one these tests pin down.+_BIG_PRIMARY_DOC = _PRIMARY_DOC.replace(+ b"<tableValueTotal>102000<", b"<tableValueTotal>102000000000<"+)+++def _cover_page(total: int) -> bytes:+ """Render a cover page reporting ``total`` as its ``tableValueTotal``."""+ return _PRIMARY_DOC.replace(b"<tableValueTotal>102000<", f"<tableValueTotal>{total}<".encode())+++def _router(*, current: bytes | None = None, prior: bytes | None = None, filings: List[Dict[str, Any]] | None = None):+ """Return a ``_sec_get`` stub that answers by URL, as the live endpoints do.++ Full-text search is filtered by the ``ciks`` parameter the way the live+ endpoint is, so a per-filer index lookup cannot accidentally see another+ filer's accessions.+ """+ current = current if current is not None else _info_table(_CURRENT_ROWS)+ prior = prior if prior is not None else _info_table(_PRIOR_ROWS)+ hits = filings if filings is not None else [+ _fts_hit(),+ _fts_hit(adsh="0000950123-26-000001", file_date="2026-02-14", period_ending="2025-12-31"),+ ]++ def fake(url: str, params: Dict[str, Any] | None = None) -> _Response:+ if url == iht._FTS_URL:+ wanted = (params or {}).get("ciks")+ matched = [h for h in hits if not wanted or wanted in (h["_source"].get("ciks") or [])]+ return _Response(payload=_fts_payload(matched, total=len(hits)))+ if url.endswith("/index.json"):+ return _Response(payload=_INDEX_JSON)+ if url.endswith("primary_doc.xml"):+ return _Response(content=_PRIMARY_DOC)+ # Archive paths carry the accession with its dashes stripped, so the+ # dashed form never appears in a document URL.+ if "000119312526226661" in url:+ return _Response(content=current)+ return _Response(content=prior)++ return fake+++def _chain_router(entries: List[Dict[str, Any]], *, cik: str = "0001067983"):+ """Return a ``_sec_get`` stub serving a whole quarter's chain of filings.++ Each entry is ``{adsh, form, file_date, cover, rows}``: ``cover`` is the+ ``primary_doc.xml`` bytes and ``rows`` the information-table rows, so a test+ can lay out an original plus any number of amendments and assert on what the+ tool makes of them.+ """+ hits = [+ _fts_hit(ciks=[cik], adsh=e["adsh"], form=e.get("form", "13F-HR"), file_date=e["file_date"])+ for e in entries+ ]+ by_accession = {e["adsh"].replace("-", ""): e for e in entries}++ def fake(url: str, params: Dict[str, Any] | None = None) -> _Response:+ if url == iht._FTS_URL:+ return _Response(payload=_fts_payload(hits))+ key = next((k for k in by_accession if k in url), None)+ if key is None:+ raise AssertionError(f"unexpected URL {url}")+ entry = by_accession[key]+ if url.endswith("/index.json"):+ return _Response(payload={"directory": {"item": [{"name": "infotable.xml", "size": "4096"}]}})+ if url.endswith("primary_doc.xml"):+ return _Response(content=entry["cover"])+ return _Response(content=_info_table(entry["rows"]))++ return fake+++# DZ BANK AG's real 2020-09-30 quarter, shrunk to four securities: an original,+# a RESTATEMENT that drops one holding and re-prices the rest, and a NEW+# HOLDINGS amendment carrying the two ETF positions the original omitted.+_DZ_ORIGINAL_ROWS = [+ {"issuer": "APPLE INC", "cusip": "037833100", "value": 25000, "shares": 100},+ {"issuer": "COCA COLA CO", "cusip": "191216100", "value": 12000, "shares": 200},+ {"issuer": "TESLA INC", "cusip": "88160R101", "value": 7000, "shares": 40},+]+_DZ_RESTATED_ROWS = [+ {"issuer": "APPLE INC", "cusip": "037833100", "value": 26000, "shares": 100},+ {"issuer": "COCA COLA CO", "cusip": "191216100", "value": 12000, "shares": 200},+]+_DZ_NEW_HOLDINGS_ROWS = [+ {"issuer": "ISHARES TR", "cls": "TIPS BD ETF", "cusip": "464287176", "value": 419548, "shares": 3310},+ {"issuer": "ISHARES TR", "cls": "0-5 YR TIPS ETF", "cusip": "46429B747", "value": 746351, "shares": 7160},+]+++def _dz_chain(*, include_new_holdings: bool = True) -> List[Dict[str, Any]]:+ """Build the DZ BANK original / RESTATEMENT / NEW HOLDINGS chain."""+ chain = [+ {+ "adsh": "0001567619-20-019792",+ "form": "13F-HR",+ "file_date": "2020-11-16",+ "cover": _PRIMARY_DOC,+ "rows": _DZ_ORIGINAL_ROWS,+ },+ {+ "adsh": "0000905148-26-002029",+ "form": "13F-HR/A",+ "file_date": "2026-05-04",+ "cover": _amended_cover("RESTATEMENT", number=1),+ "rows": _DZ_RESTATED_ROWS,+ },+ ]+ if include_new_holdings:+ chain.append(+ {+ "adsh": "0000905148-26-002030",+ "form": "13F-HR/A",+ "file_date": "2026-05-04",+ "cover": _amended_cover("NEW HOLDINGS", number=2),+ "rows": _DZ_NEW_HOLDINGS_ROWS,+ }+ )+ return chain+++class TestQuarterParsing:+ """``quarter`` accepts the documented forms and rejects everything else."""++ @pytest.mark.parametrize(+ ("raw", "expected"),+ [+ ("2026Q1", "2026-03-31"),+ ("2026q4", "2026-12-31"),+ ("2026-Q2", "2026-06-30"),+ ("2026-09-30", "2026-09-30"),+ ("2025-6-30", "2025-06-30"),+ ],+ )+ def test_accepted_forms(self, raw: str, expected: str) -> None:+ assert iht._parse_quarter(raw) == expected++ @pytest.mark.parametrize("raw", ["2026Q5", "26Q1", "2026-02-28", "last quarter", "", "1980Q1"])+ def test_rejected_forms(self, raw: str) -> None:+ with pytest.raises(ValueError):+ iht._parse_quarter(raw)++ def test_bad_quarter_reaches_the_user_as_an_envelope(self) -> None:+ tool = InstitutionalHoldingsTool()+ payload = json.loads(tool.execute(mode="manager_holdings", manager="X", quarter="2026Q7"))+ assert payload["ok"] is False+ assert "2026Q7" in payload["error"]++ def test_latest_reportable_quarter_waits_out_the_filing_lag(self) -> None:+ from datetime import date++ # 2026-08-04 is only 35 days past 2026-06-30, inside the 45-day window,+ # so the newest fully-filed quarter is still Q1.+ assert iht._latest_reportable_quarter(date(2026, 8, 4)) == "2026-03-31"+ assert iht._latest_reportable_quarter(date(2026, 8, 20)) == "2026-06-30"+++class TestValueUnits:+ """``value`` is thousands on old filings and dollars on new ones — usually."""++ def test_modern_filing_is_dollars(self) -> None:+ multiplier, units, basis = iht._detect_value_units(+ iht._parse_information_table(_info_table(_CURRENT_ROWS)), "2026-05-15"+ )+ assert (multiplier, units) == (1, "usd")+ assert "filing_date" in basis++ def test_pre_2023_filing_is_thousands(self) -> None:+ rows = [{"issuer": "APPLE INC", "cusip": "037833100", "value": 138, "shares": 1000}] * 6+ multiplier, units, _ = iht._detect_value_units(iht._parse_information_table(_info_table(rows)), "2022-11-14")+ assert (multiplier, units) == (1000, "usd_thousands")++ def test_implied_price_overrides_a_modern_filing_still_using_thousands(self) -> None:+ """BancFirst filed on 2023-01-03 in thousands; the date prior alone is wrong by 1000x."""+ rows = [{"issuer": "EXXON MOBIL CORP", "cusip": "30231G102", "value": 34646, "shares": 317269}] * 6+ parsed = iht._parse_information_table(_info_table(rows))+ multiplier, units, basis = iht._detect_value_units(parsed, "2023-01-03")+ assert (multiplier, units) == (1000, "usd_thousands")+ assert basis.startswith("implied_price_override")++ def test_implied_price_overrides_an_old_filing_already_using_dollars(self) -> None:+ rows = [{"issuer": "APPLE INC", "cusip": "037833100", "value": 138200, "shares": 1000}] * 6+ parsed = iht._parse_information_table(_info_table(rows))+ multiplier, units, basis = iht._detect_value_units(parsed, "2022-11-14")+ assert (multiplier, units) == (1, "usd")+ assert basis.startswith("implied_price_override")++ def test_too_few_rows_falls_back_to_the_date_alone(self) -> None:+ rows = [{"issuer": "APPLE INC", "cusip": "037833100", "value": 1, "shares": 100}]+ _, _, basis = iht._detect_value_units(iht._parse_information_table(_info_table(rows)), "2026-05-15")+ assert basis == "filing_date"++ def test_principal_rows_are_excluded_from_the_price_check(self) -> None:+ """A bond row's value/principal ratio is not a share price and must not vote."""+ rows = [{"issuer": "SOME BOND", "cusip": "111111111", "value": 1, "shares": 1000, "type": "PRN"}] * 8+ _, _, basis = iht._detect_value_units(iht._parse_information_table(_info_table(rows)), "2026-05-15")+ assert basis == "filing_date"+++class TestInformationTableParsing:+ """The XML reader handles real-world encodings and refuses the wrong document."""++ def test_rows_carry_every_reported_field(self) -> None:+ rows = iht._parse_information_table(+ _info_table(+ [{"issuer": "APPLE INC", "cusip": "037833100", "value": 25000, "shares": 100, "put_call": "Put"}]+ )+ )+ assert rows == [+ {+ "issuer": "APPLE INC",+ "title_of_class": "COM",+ "cusip": "037833100",+ "value_raw": 25000.0,+ "shares": 100.0,+ "share_type": "SH",+ "put_call": "Put",+ "discretion": "SOLE",+ "other_manager": None,+ "voting_sole": 100.0,+ "voting_shared": 0.0,+ "voting_none": 0.0,+ }+ ]++ def test_non_utf8_declaration_is_honoured(self) -> None:+ """Filers do emit windows-1252 declarations; parsing bytes is what makes that work."""+ raw = _info_table(_CURRENT_ROWS, declaration='<?xml version="1.0" encoding="windows-1252"?>')+ assert len(iht._parse_information_table(raw)) == len(_CURRENT_ROWS)++ def test_cusip_filter_keeps_only_the_requested_security(self) -> None:+ rows = iht._parse_information_table(_info_table(_CURRENT_ROWS), cusip_filter="037833100")+ assert len(rows) == 2+ assert {r["cusip"] for r in rows} == {"037833100"}++ def test_primary_doc_is_rejected_by_its_root_tag(self) -> None:+ with pytest.raises(ValueError, match="informationTable"):+ iht._parse_information_table(_PRIMARY_DOC)++ def test_row_cap_bounds_a_huge_table(self) -> None:+ rows = [{"issuer": "X", "cusip": "037833100", "value": 1, "shares": 1}] * 5+ with patch.object(iht, "_MAX_INFOTABLE_ROWS", 3):+ assert len(iht._parse_information_table(_info_table(rows))) == 3+++class TestAggregation:+ """Duplicate rows for one CUSIP collapse into a single position."""++ def test_subadviser_rows_merge_into_one_position(self) -> None:+ positions = iht._aggregate(iht._parse_information_table(_info_table(_CURRENT_ROWS)), 1)+ apple = next(p for p in positions if p["cusip"] == "037833100")+ assert apple["shares"] == 300.0+ assert apple["value_usd"] == 75000.0+ assert apple["rows"] == 2+ assert [p["cusip"] for p in positions] == sorted(+ (p["cusip"] for p in positions), key=lambda c: -next(x["value_usd"] for x in positions if x["cusip"] == c)+ )++ def test_multiplier_and_weights_are_applied(self) -> None:+ positions = iht._aggregate(iht._parse_information_table(_info_table(_CURRENT_ROWS)), 1000)+ assert positions[0]["value_usd"] == 75_000_000.0+ assert round(sum(p["weight_pct"] for p in positions), 4) == 100.0++ def test_puts_and_calls_stay_separate_from_the_common_stock(self) -> None:+ rows = [+ {"issuer": "APPLE INC", "cusip": "037833100", "value": 100, "shares": 10},+ {"issuer": "APPLE INC", "cusip": "037833100", "value": 50, "shares": 5, "put_call": "Put"},+ ]+ positions = iht._aggregate(iht._parse_information_table(_info_table(rows)), 1)+ assert len(positions) == 2+ assert {p["put_call"] for p in positions} == {None, "Put"}+++class TestChanges:+ """Quarter-over-quarter diffs classify every move and drop the unchanged."""++ def test_all_four_actions_are_detected(self) -> None:+ current = iht._aggregate(iht._parse_information_table(_info_table(_CURRENT_ROWS)), 1)+ prior = iht._aggregate(iht._parse_information_table(_info_table(_PRIOR_ROWS)), 1)+ by_cusip = {c["cusip"]: c for c in iht._diff_positions(current, prior)}++ assert by_cusip["037833100"]["action"] == "reduced"+ assert by_cusip["037833100"]["shares_change"] == -100.0+ assert by_cusip["037833100"]["shares_change_pct"] == -25.0+ assert by_cusip["88160R101"]["action"] == "closed"+ assert by_cusip["88160R101"]["shares_after"] == 0.0+ # NVIDIA's value moved but its share count did not: a mark-to-market+ # move is not a trade, so it must not be reported as one.+ assert "67066G104" not in by_cusip+ assert "191216100" not in by_cusip++ def test_a_brand_new_position_is_flagged_new(self) -> None:+ current = iht._aggregate(iht._parse_information_table(_info_table(_CURRENT_ROWS)), 1)+ changes = iht._diff_positions(current, [])+ assert {c["action"] for c in changes} == {"new"}+ assert all(c["shares_before"] == 0.0 for c in changes)+ assert all(c["shares_change_pct"] is None for c in changes)++ def test_changes_are_ordered_by_dollar_impact(self) -> None:+ current = iht._aggregate(iht._parse_information_table(_info_table(_CURRENT_ROWS)), 1)+ changes = iht._diff_positions(current, [])+ impacts = [abs(c["value_usd_change"]) for c in changes]+ assert impacts == sorted(impacts, reverse=True)++ def test_the_diff_itself_is_uncapped_so_counts_stay_truthful(self) -> None:+ """Trimming belongs to the budget step; capping here would corrupt the totals."""+ current = iht._aggregate(+ iht._parse_information_table(+ _info_table([{"issuer": "X", "cusip": f"{i:09d}", "value": 100 + i, "shares": 5} for i in range(300)])+ ),+ 1,+ )+ assert len(iht._diff_positions(current, [])) == 300++ def test_fit_changes_trims_to_the_reserved_share(self) -> None:+ current = iht._aggregate(+ iht._parse_information_table(+ _info_table([{"issuer": "X", "cusip": f"{i:09d}", "value": 100 + i, "shares": 5} for i in range(300)])+ ),+ 1,+ )+ kept = iht._fit_changes(iht._diff_positions(current, []))+ assert 0 < len(kept) < 300+ assert len(json.dumps(kept)) <= TOOL_RESULT_LIMIT * iht._CHANGES_BUDGET_SHARE+++class TestManagerHoldingsMode:+ """The default mode returns a ranked portfolio and an honest envelope."""++ def test_success_envelope(self) -> None:+ with patch.object(iht, "_sec_get", side_effect=_router()):+ payload = json.loads(+ InstitutionalHoldingsTool().execute(manager="0001067983", quarter="2026Q1", top_n=3)+ )++ assert payload["ok"] is True+ assert payload["market"] == "US"+ assert payload["source"] == "sec_edgar_13f"+ assert payload["mode"] == "manager_holdings"+ data = payload["data"]+ assert data["cik"] == "0001067983"+ assert data["position_count"] == 5+ assert data["portfolio_value_usd"] == 102000.0+ assert data["filing"]["quarter"] == "2026Q1"+ assert data["filing"]["value_units"] == "usd"+ assert len(data["positions"]) == 3+ assert data["positions"][0]["issuer"] == "APPLE INC"++ def test_a_top_n_capped_list_does_not_pass_itself_off_as_complete(self) -> None:+ """``paging.complete`` covers the ranked slice, not the whole portfolio."""+ with patch.object(iht, "_sec_get", side_effect=_router()):+ payload = json.loads(+ InstitutionalHoldingsTool().execute(manager="0001067983", quarter="2026Q1", top_n=2)+ )++ data = payload["data"]+ assert payload["paging"]["complete"] is True, "the 2-record slice is fully delivered"+ assert data["position_count"] == 5, "but the filer really reported 5 positions"+ assert data["positions_truncated"] is True+ assert data["positions_ranked_limit"] == 2++ def test_an_uncapped_list_is_not_flagged_as_truncated(self) -> None:+ with patch.object(iht, "_sec_get", side_effect=_router()):+ payload = json.loads(+ InstitutionalHoldingsTool().execute(manager="0001067983", quarter="2026Q1", top_n=50)+ )+ assert payload["data"]["positions_truncated"] is False++ def test_portfolio_total_matches_the_cover_page(self) -> None:+ """Aggregated positions must reconcile to the filer's own reported total."""+ with patch.object(iht, "_sec_get", side_effect=_router()):+ payload = json.loads(InstitutionalHoldingsTool().execute(manager="0001067983", quarter="2026Q1"))+ summary = iht._fetch_summary("0001067983", "0001193125-26-226661")+ assert payload["data"]["portfolio_value_usd"] == summary["total_value_raw"]++ def test_include_changes_diffs_against_the_previous_quarter(self) -> None:+ with patch.object(iht, "_sec_get", side_effect=_router()):+ payload = json.loads(+ InstitutionalHoldingsTool().execute(manager="0001067983", quarter="2026Q1", include_changes=True)+ )+ changes = payload["data"]["changes"]+ assert changes["prior_quarter"] == "2025Q4"+ assert changes["counts"] == {"reduced": 1, "closed": 1}++ def test_a_busy_diff_stays_inside_the_result_budget(self) -> None:+ """The change list rides outside the paged position list, so it needs its own cap.++ Measured live before this was bounded: Bridgewater's 2026Q1 diff+ serialized to 60,419 characters against a 10,000-character budget, which+ the agent loop would have cut mid-structure.+ """+ current = [+ {"issuer": f"ISSUER {i:04d} INC OF DELAWARE", "cusip": f"{i:09d}", "value": 9_000 - i, "shares": 70}+ for i in range(300)+ ]+ prior = [{**row, "shares": 40, "value": row["value"] // 2} for row in current[:250]]++ with patch.object(iht, "_sec_get", side_effect=_router(current=_info_table(current), prior=_info_table(prior))):+ out = InstitutionalHoldingsTool().execute(manager="0001067983", quarter="2026Q1", include_changes=True)++ payload = json.loads(out)+ changes = payload["data"]["changes"]+ assert len(out) <= TOOL_RESULT_LIMIT+ assert changes["truncated"] is True+ assert changes["returned"] < changes["total"]+ # Per-action totals are computed before trimming, so they must still+ # describe the whole quarter rather than only the surviving rows.+ assert sum(changes["counts"].values()) == changes["total"] == 300+ assert changes["counts"]["new"] == 50+ assert changes["counts"]["increased"] == 250++ def test_changes_report_their_own_absence(self) -> None:+ with patch.object(iht, "_sec_get", side_effect=_router(filings=[_fts_hit()])):+ payload = json.loads(+ InstitutionalHoldingsTool().execute(manager="0001067983", quarter="2026Q1", include_changes=True)+ )+ assert "no earlier 13F-HR filing" in payload["data"]["changes"]["error"]+++ def test_manager_name_resolves_through_full_text_search(self) -> None:+ captured: List[Dict[str, Any]] = []+ router = _router()++ def fake(url: str, params: Dict[str, Any] | None = None) -> _Response:+ if url == iht._FTS_URL:+ captured.append(params or {})+ return router(url, params)++ with patch.object(iht, "_sec_get", side_effect=fake):+ payload = json.loads(InstitutionalHoldingsTool().execute(manager="Test Capital"))++ assert captured[0]["entityName"] == "Test Capital"+ assert captured[0]["forms"] == "13F-HR"+ assert payload["data"]["cik"] == "0001067983"++ def test_ambiguous_manager_names_are_surfaced_not_hidden(self) -> None:+ hits = [+ _fts_hit(ciks=["0001350694"], display_names=["Bridgewater Associates, LP (CIK 0001350694)"]),+ _fts_hit(ciks=["0001600319"], display_names=["Bridgewater Advisors Inc. (CIK 0001600319)"]),+ ]++ resolved = _router(filings=[_fts_hit(ciks=["0001350694"])])++ def fake(url: str, params: Dict[str, Any] | None = None) -> _Response:+ if url == iht._FTS_URL and (params or {}).get("entityName"):+ return _Response(payload=_fts_payload(hits))+ return resolved(url, params)++ with patch.object(iht, "_sec_get", side_effect=fake):+ payload = json.loads(InstitutionalHoldingsTool().execute(manager="Bridgewater"))++ assert payload["data"]["cik"] == "0001350694"+ assert payload["data"]["other_name_matches"] == [+ {"cik": "0001600319", "name": "Bridgewater Advisors Inc. (CIK 0001600319)"}+ ]++ def test_pre_xml_filing_reports_why_it_cannot_be_read(self) -> None:+ """Berkshire's 2011 filing holds one .txt document and no XML at all."""+ text_only = {"directory": {"item": [{"name": "d254132d13fhr.txt", "size": "20492"}]}}++ def fake(url: str, params: Dict[str, Any] | None = None) -> _Response:+ if url == iht._FTS_URL:+ return _Response(payload=_fts_payload([_fts_hit()]))+ if url.endswith("/index.json"):+ return _Response(payload=text_only)+ raise AssertionError(f"no document should be fetched, got {url}")++ with patch.object(iht, "_sec_get", side_effect=fake):+ payload = json.loads(InstitutionalHoldingsTool().execute(manager="0001067983"))++ assert payload["ok"] is False+ assert "pre-2013" in payload["error"]++ def test_oversized_information_table_is_skipped_with_a_reason(self) -> None:+ huge = {"directory": {"item": [{"name": "big.xml", "size": str(999 * 1024 * 1024)}]}}++ def fake(url: str, params: Dict[str, Any] | None = None) -> _Response:+ if url == iht._FTS_URL:+ return _Response(payload=_fts_payload([_fts_hit()]))+ return _Response(payload=huge)++ with patch.object(iht, "_sec_get", side_effect=fake):+ payload = json.loads(InstitutionalHoldingsTool().execute(manager="0001067983"))++ assert payload["ok"] is False+ assert "ceiling" in payload["error"]++ def test_an_unrelated_xml_candidate_does_not_abort_the_filing(self) -> None:+ """Only the root tag identifies the table, so a decoy XML must be skipped."""+ index = {"directory": {"item": [{"name": "decoy.xml", "size": "10"}, {"name": "53405.xml", "size": "45259"}]}}++ def fake(url: str, params: Dict[str, Any] | None = None) -> _Response:+ if url == iht._FTS_URL:+ return _Response(payload=_fts_payload([_fts_hit()]))+ if url.endswith("/index.json"):+ return _Response(payload=index)+ if url.endswith("decoy.xml"):+ return _Response(content=b"<somethingElse/>")+ return _Response(content=_info_table(_CURRENT_ROWS))++ with patch.object(iht, "_sec_get", side_effect=fake):+ payload = json.loads(InstitutionalHoldingsTool().execute(manager="0001067983"))++ assert payload["ok"] is True+ assert payload["data"]["filing"]["document_url"].endswith("53405.xml")++ def test_submissions_fallback_when_full_text_search_is_down(self) -> None:+ submissions = {+ "filings": {+ "recent": {+ "form": ["10-K", "13F-HR"],+ "accessionNumber": ["x", "0001193125-26-226661"],+ "filingDate": ["2026-01-01", "2026-05-15"],+ "reportDate": ["2025-12-31", "2026-03-31"],+ }+ }+ }++ def fake(url: str, params: Dict[str, Any] | None = None) -> _Response:+ if url == iht._FTS_URL and (params or {}).get("ciks"):+ raise RuntimeError("full-text search unavailable")+ return _router()(url, params)++ with patch.object(iht, "_sec_get", side_effect=fake), patch.object(+ iht, "get_submissions", return_value=submissions+ ):+ payload = json.loads(InstitutionalHoldingsTool().execute(manager="0001067983", quarter="2026Q1"))++ assert payload["ok"] is True+ assert payload["data"]["filing"]["accession"] == "0001193125-26-226661"++ def test_missing_quarter_says_how_far_back_it_looked(self) -> None:+ with patch.object(iht, "_sec_get", side_effect=_router()):+ payload = json.loads(InstitutionalHoldingsTool().execute(manager="0001067983", quarter="2011Q3"))+ assert payload["ok"] is False+ assert "most recent" in payload["error"]+ assert payload["available_quarters"] == ["2026-03-31", "2025-12-31"]+++class TestAmendmentClassification:+ """``amendmentType`` decides everything, and neither signal alone is trusted."""++ @pytest.mark.parametrize(+ ("form", "cover", "expected"),+ [+ ("13F-HR", _PRIMARY_DOC, iht._ROLE_ORIGINAL),+ ("13F-HR/A", _amended_cover("RESTATEMENT"), iht._ROLE_RESTATEMENT),+ ("13F-HR/A", _amended_cover("NEW HOLDINGS"), iht._ROLE_NEW_HOLDINGS),+ # AEGON ASSET MANAGEMENT UK Plc: submitted as /A, cover page silent.+ ("13F-HR/A", _untyped_amendment_cover(), iht._ROLE_UNSPECIFIED),+ # Nova Wealth Management: submitted as plain 13F-HR, cover page says+ # NEW HOLDINGS. The cover page wins over the missing suffix.+ ("13F-HR", _amended_cover("NEW HOLDINGS"), iht._ROLE_NEW_HOLDINGS),+ # A value outside the two SEC filers actually use must not be mapped+ # onto either behaviour.+ ("13F-HR/A", _amended_cover("PARTIAL WITHDRAWAL"), iht._ROLE_UNSPECIFIED),+ ],+ )+ def test_role_comes_from_both_signals(self, form: str, cover: bytes, expected: str) -> None:+ with patch.object(iht, "_sec_get", return_value=_Response(content=cover)):+ summary = iht._fetch_summary("0001067983", "0001193125-26-226661")+ verdict = iht._classify_filing({"form": form}, summary)++ assert verdict["role"] == expected+ assert verdict["is_amendment"] is (expected != iht._ROLE_ORIGINAL)+ # Whatever the verdict, the evidence behind it has to be legible.+ assert "submissionType" in verdict["role_basis"]+ assert "coverPage.amendmentType" in verdict["role_basis"]++ def test_an_unreadable_cover_page_falls_back_to_the_form_and_says_so(self) -> None:+ verdict = iht._classify_filing({"form": "13F-HR/A"}, {"error": "connection reset"})+ assert verdict["role"] == iht._ROLE_UNSPECIFIED+ assert verdict["amendment_type_known"] is False+ assert "coverPage=unreadable" in verdict["role_basis"]++ def test_amendment_number_orders_two_same_day_amendments(self) -> None:+ """DZ BANK filed both of its 2020Q3 amendments on 2026-05-04."""+ with patch.object(iht, "_sec_get", side_effect=_chain_router(_dz_chain())):+ filings = iht._filings_for_period(iht._list_13f_filings("0001067983"), "2026-03-31")+ chain, truncated = iht._quarter_chain("0001067983", filings)++ assert truncated is False+ assert [f["role"] for f in chain] == [+ iht._ROLE_ORIGINAL,+ iht._ROLE_RESTATEMENT,+ iht._ROLE_NEW_HOLDINGS,+ ]+ assert [f["amendment_no"] for f in chain] == [None, 1.0, 2.0]++ def test_true_and_false_flags_are_read_but_a_missing_one_stays_unknown(self) -> None:+ assert iht._to_bool("true") is True+ assert iht._to_bool("Y") is True+ assert iht._to_bool("false") is False+ assert iht._to_bool("N") is False+ assert iht._to_bool(None) is None, "absent is a third state, never a silent False"+ assert iht._to_bool("maybe") is None+++class TestAmendmentChainResolution:+ """A quarter is the whole chain of filings, not whichever one arrived first."""++ def test_restatement_replaces_and_new_holdings_merges(self) -> None:+ with patch.object(iht, "_sec_get", side_effect=_chain_router(_dz_chain())):+ payload = json.loads(+ InstitutionalHoldingsTool().execute(manager="0001067983", quarter="2026Q1", top_n=50)+ )++ data = payload["data"]+ held = {p["cusip"]: p for p in data["positions"]}+ # The restatement dropped Tesla, so it must be gone despite the original.+ assert "88160R101" not in held+ # ... and re-priced Apple, so the restated number must win.+ assert held["037833100"]["value_usd"] == 26000.0+ # The NEW HOLDINGS rows are additions, not a replacement portfolio.+ assert held["464287176"]["value_usd"] == 419548.0+ assert held["46429B747"]["value_usd"] == 746351.0+ assert data["position_count"] == 4+ assert data["portfolio_value_usd"] == 26000.0 + 12000.0 + 419548.0 + 746351.0++ resolution = data["amendment_resolution"]+ assert resolution["resolution"] == "restated_then_extended"+ assert resolution["unresolved_amendments"] == 0+ assert [(f["role"], f["applied"], f.get("effect")) for f in resolution["filings"]] == [+ (iht._ROLE_ORIGINAL, False, None),+ (iht._ROLE_RESTATEMENT, True, "replace"),+ (iht._ROLE_NEW_HOLDINGS, True, "merge"),+ ]+ assert resolution["filings"][0]["not_applied_reason"] == (+ "superseded by a later RESTATEMENT for the same quarter"+ )++ def test_a_superseded_table_is_never_downloaded(self) -> None:+ """Reading a table only to throw it away costs a full document fetch."""+ fetched: List[str] = []+ router = _chain_router(_dz_chain())++ def fake(url: str, params: Dict[str, Any] | None = None) -> _Response:+ if url != iht._FTS_URL:+ fetched.append(url)+ return router(url, params)++ with patch.object(iht, "_sec_get", side_effect=fake):+ InstitutionalHoldingsTool().execute(manager="0001067983", quarter="2026Q1")++ superseded = "0001567619-20-019792".replace("-", "")+ assert not any(u.endswith("infotable.xml") and superseded in u for u in fetched)+ assert any(u.endswith("primary_doc.xml") and superseded in u for u in fetched), (+ "its cover page is still read — that is what proves it was superseded"+ )++ def test_an_unreadable_restatement_falls_back_to_what_it_superseded(self) -> None:+ """Skipping superseded tables must never turn one bad fetch into no answer."""+ router = _chain_router(_dz_chain(include_new_holdings=False))+ restatement = "0000905148-26-002029".replace("-", "")++ def fake(url: str, params: Dict[str, Any] | None = None) -> _Response:+ if restatement in url and url.endswith("infotable.xml"):+ raise OSError("connection reset")+ return router(url, params)++ with patch.object(iht, "_sec_get", side_effect=fake):+ payload = json.loads(+ InstitutionalHoldingsTool().execute(manager="0001067983", quarter="2026Q1", top_n=50)+ )++ data = payload["data"]+ assert data["position_count"] == 3, "the original's three rows, not an error"+ assert data["filing"]["accession"] == "0001567619-20-019792"+ failed = [f for f in data["amendment_resolution"]["filings"] if f.get("error")]+ assert "connection reset" in failed[0]["error"]++ def test_an_unreadable_restatement_falls_back_even_when_new_holdings_stands_in(self) -> None:+ """The fallback must key on 'no restatement applied', not on 'no answer at all'.++ DZ BANK's real chain is original + RESTATEMENT + NEW HOLDINGS. With the+ NEW HOLDINGS leg present a failed restatement fetch still leaves *an*+ answer standing, so an error-only retry guard never fires and the 2-row+ fragment is returned while the original is skipped undownloaded.+ """+ router = _chain_router(_dz_chain(include_new_holdings=True))+ restatement = "0000905148-26-002029".replace("-", "")++ def fake(url: str, params: Dict[str, Any] | None = None) -> _Response:+ if restatement in url and url.endswith("infotable.xml"):+ raise OSError("connection reset")+ return router(url, params)++ with patch.object(iht, "_sec_get", side_effect=fake):+ payload = json.loads(+ InstitutionalHoldingsTool().execute(manager="0001067983", quarter="2026Q1", top_n=50)+ )++ data = payload["data"]+ resolution = data["amendment_resolution"]+ assert resolution["resolution"] == "original_plus_new_holdings"+ assert data["position_count"] == 5, "the original's three rows plus the two merged in"+ assert data["filing"]["accession"] == "0001567619-20-019792"+ assert [f["applied"] for f in resolution["filings"]] == [True, False, True]++ def test_a_fragment_never_claims_the_base_filing_was_absent(self) -> None:+ """'No base filing was found' is a different fact from 'none could be read'."""+ chain = _dz_chain(include_new_holdings=True)+ del chain[1] # original + NEW HOLDINGS, no restatement to fall back through+ router = _chain_router(chain)+ original = "0001567619-20-019792".replace("-", "")++ def fake(url: str, params: Dict[str, Any] | None = None) -> _Response:+ if original in url and url.endswith("infotable.xml"):+ raise OSError("connection reset")+ return router(url, params)++ with patch.object(iht, "_sec_get", side_effect=fake):+ payload = json.loads(+ InstitutionalHoldingsTool().execute(manager="0001067983", quarter="2026Q1", top_n=50)+ )++ resolution = payload["data"]["amendment_resolution"]+ assert resolution["resolution"] == "new_holdings_without_base"+ assert any(f["role"] == "original" for f in resolution["filings"]), "the base filing IS listed"+ assert "no base filing was found" not in resolution["note"]+ assert "none could be read" in resolution["note"]+ assert "FRAGMENT" in resolution["note"]++ def test_the_standing_filing_is_the_restatement_not_the_original(self) -> None:+ """Downstream has to be able to tell which document the answer stands on."""+ with patch.object(iht, "_sec_get", side_effect=_chain_router(_dz_chain())):+ payload = json.loads(InstitutionalHoldingsTool().execute(manager="0001067983", quarter="2026Q1"))++ filing = payload["data"]["filing"]+ assert filing["accession"] == "0000905148-26-002029"+ assert filing["form"] == "13F-HR/A"++ def test_a_restatement_alone_replaces_the_original(self) -> None:+ with patch.object(iht, "_sec_get", side_effect=_chain_router(_dz_chain(include_new_holdings=False))):+ payload = json.loads(+ InstitutionalHoldingsTool().execute(manager="0001067983", quarter="2026Q1", top_n=50)+ )++ data = payload["data"]+ assert data["amendment_resolution"]["resolution"] == "restated"+ assert data["position_count"] == 2+ assert data["portfolio_value_usd"] == 38000.0++ def test_new_holdings_on_top_of_an_untouched_original(self) -> None:+ chain = [_dz_chain()[0], _dz_chain()[2]]+ with patch.object(iht, "_sec_get", side_effect=_chain_router(chain)):+ payload = json.loads(+ InstitutionalHoldingsTool().execute(manager="0001067983", quarter="2026Q1", top_n=50)+ )++ data = payload["data"]+ assert data["amendment_resolution"]["resolution"] == "original_plus_new_holdings"+ assert data["position_count"] == 5, "3 original + 2 added, none replaced"+ assert data["filing"]["accession"] == "0001567619-20-019792"++ def test_filings_in_different_value_units_merge_correctly(self) -> None:+ """A 2020 original in thousands, amended in 2026 in whole dollars.++ DZ BANK's real chain is exactly this: 41,697,805 (thousands) restated as+ 41,684,874,610 (dollars). Each filing is scaled to USD on its own before+ anything is merged, so the two never get added in different units.+ """+ chain = [+ {+ "adsh": "0001567619-20-019792",+ "form": "13F-HR",+ "file_date": "2020-11-16",+ "cover": _PRIMARY_DOC,+ # 6 rows priced around $250/share once the thousands multiplier+ # is applied, which is what settles the unit check.+ "rows": [+ {"issuer": f"ISSUER {i}", "cusip": f"00000000{i}", "value": 25, "shares": 100}+ for i in range(6)+ ],+ },+ {+ "adsh": "0000905148-26-002030",+ "form": "13F-HR/A",+ "file_date": "2026-05-04",+ "cover": _amended_cover("NEW HOLDINGS", number=1),+ "rows": [{"issuer": "ISHARES TR", "cusip": "464287176", "value": 419548, "shares": 3310}],+ },+ ]+ with patch.object(iht, "_sec_get", side_effect=_chain_router(chain)):+ payload = json.loads(+ InstitutionalHoldingsTool().execute(manager="0001067983", quarter="2026Q1", top_n=50)+ )++ data = payload["data"]+ sources = {f["accession"]: f for f in data["amendment_resolution"]["filings"]}+ assert sources["0001567619-20-019792"]["value_units"] == "usd_thousands"+ assert sources["0000905148-26-002030"]["value_units"] == "usd"+ # 6 x 25 thousand = 150,000 USD, plus the amendment's 419,548 USD.+ assert data["portfolio_value_usd"] == 150_000.0 + 419_548.0++ def test_an_untyped_amendment_is_reported_unresolved_not_guessed(self) -> None:+ """AEGON files 13F-HR/A with no amendmentType; neither behaviour is assumed."""+ chain = [+ {+ "adsh": "0001539994-25-000009",+ "form": "13F-HR",+ "file_date": "2025-07-28",+ "cover": _PRIMARY_DOC,+ "rows": _DZ_ORIGINAL_ROWS,+ },+ {+ "adsh": "0001539994-26-000022",+ "form": "13F-HR/A",+ "file_date": "2026-04-10",+ "cover": _untyped_amendment_cover(),+ "rows": _DZ_NEW_HOLDINGS_ROWS,+ },+ ]+ with patch.object(iht, "_sec_get", side_effect=_chain_router(chain)):+ payload = json.loads(+ InstitutionalHoldingsTool().execute(manager="0001067983", quarter="2026Q1", top_n=50)+ )++ data = payload["data"]+ resolution = data["amendment_resolution"]+ assert resolution["resolution"] == "incomplete_unspecified_amendment"+ assert resolution["unresolved_amendments"] == 1+ assert "UNKNOWN" in resolution["note"]+ # Neither merged nor substituted: the base stands and the amendment is+ # described well enough for the caller to go and read it.+ assert data["position_count"] == 3+ unresolved = [f for f in resolution["filings"] if not f["applied"]]+ assert unresolved[0]["row_count"] == 2+ assert unresolved[0]["document_url"].endswith("infotable.xml")+ assert unresolved[0]["amendment_type_known"] is False++ def test_a_new_holdings_amendment_with_no_base_is_called_a_fragment(self) -> None:+ """Nova Wealth Management's only 2026Q1 filing declares NEW HOLDINGS."""+ chain = [+ {+ "adsh": "0001376474-26-000287",+ "form": "13F-HR",+ "file_date": "2026-05-12",+ "cover": _amended_cover("NEW HOLDINGS", number=1),+ "rows": _DZ_NEW_HOLDINGS_ROWS,+ }+ ]+ with patch.object(iht, "_sec_get", side_effect=_chain_router(chain)):+ payload = json.loads(InstitutionalHoldingsTool().execute(manager="0001067983", quarter="2026Q1"))++ resolution = payload["data"]["amendment_resolution"]+ assert resolution["resolution"] == "new_holdings_without_base"+ assert "FRAGMENT" in resolution["note"]++ def test_an_untyped_amendment_alone_is_reported_but_labelled_unverified(self) -> None:+ chain = [+ {+ "adsh": "0001539994-26-000022",+ "form": "13F-HR/A",+ "file_date": "2026-04-10",+ "cover": _untyped_amendment_cover(),+ "rows": _DZ_ORIGINAL_ROWS,+ }+ ]+ with patch.object(iht, "_sec_get", side_effect=_chain_router(chain)):+ payload = json.loads(InstitutionalHoldingsTool().execute(manager="0001067983", quarter="2026Q1"))++ data = payload["data"]+ assert data["position_count"] == 3+ assert data["amendment_resolution"]["resolution"] == "unverified_amendment_only"+ assert "NOT confirmed" in data["amendment_resolution"]["note"]++ def test_a_repeated_security_in_a_new_holdings_amendment_is_counted(self) -> None:+ """Overlap should be empty by definition, so it is surfaced when it is not."""+ chain = [+ _dz_chain()[0],+ {+ "adsh": "0000905148-26-002030",+ "form": "13F-HR/A",+ "file_date": "2026-05-04",+ "cover": _amended_cover("NEW HOLDINGS", number=1),+ "rows": [{"issuer": "APPLE INC", "cusip": "037833100", "value": 5000, "shares": 20}],+ },+ ]+ with patch.object(iht, "_sec_get", side_effect=_chain_router(chain)):+ payload = json.loads(+ InstitutionalHoldingsTool().execute(manager="0001067983", quarter="2026Q1", top_n=50)+ )++ data = payload["data"]+ assert data["amendment_resolution"]["overlapping_positions"] == 1+ assert data["position_count"] == 3+ apple = next(p for p in data["positions"] if p["cusip"] == "037833100")+ assert apple["shares"] == 120.0++ def test_the_prior_quarter_of_a_diff_is_resolved_the_same_way(self) -> None:+ """Diffing against a superseded original would invent changes."""+ chain = [+ {+ "adsh": "0001193125-26-226661",+ "form": "13F-HR",+ "file_date": "2026-05-15",+ "cover": _PRIMARY_DOC,+ "rows": _DZ_RESTATED_ROWS,+ },+ {+ "adsh": "0000950123-26-000001",+ "form": "13F-HR",+ "file_date": "2026-02-14",+ "cover": _PRIMARY_DOC,+ "rows": _DZ_ORIGINAL_ROWS,+ },+ {+ "adsh": "0000950123-26-000077",+ "form": "13F-HR/A",+ "file_date": "2026-03-01",+ "cover": _amended_cover("RESTATEMENT", number=1),+ "rows": _DZ_RESTATED_ROWS,+ },+ ]+ hits = [+ _fts_hit(adsh=chain[0]["adsh"], file_date=chain[0]["file_date"]),+ _fts_hit(adsh=chain[1]["adsh"], file_date=chain[1]["file_date"], period_ending="2025-12-31"),+ _fts_hit(+ adsh=chain[2]["adsh"],+ form="13F-HR/A",+ file_date=chain[2]["file_date"],+ period_ending="2025-12-31",+ ),+ ]+ by_accession = {e["adsh"].replace("-", ""): e for e in chain}++ def fake(url: str, params: Dict[str, Any] | None = None) -> _Response:+ if url == iht._FTS_URL:+ return _Response(payload=_fts_payload(hits))+ entry = by_accession[next(k for k in by_accession if k in url)]+ if url.endswith("/index.json"):+ return _Response(payload={"directory": {"item": [{"name": "infotable.xml", "size": "4096"}]}})+ if url.endswith("primary_doc.xml"):+ return _Response(content=entry["cover"])+ return _Response(content=_info_table(entry["rows"]))++ with patch.object(iht, "_sec_get", side_effect=fake):+ payload = json.loads(+ InstitutionalHoldingsTool().execute(+ manager="0001067983", quarter="2026Q1", include_changes=True+ )+ )++ changes = payload["data"]["changes"]+ # 2025Q4 was restated to the same two rows the current quarter reports,+ # so a chain-aware diff sees no change at all. Reading 2025Q4's original+ # instead would have reported a phantom "closed TESLA".+ assert changes["prior_quarter"] == "2025Q4"+ assert changes["prior_accession"] == "0000950123-26-000077"+ assert changes["prior_amendment_resolution"]["resolution"] == "restated"+ assert changes["counts"] == {}+++class TestTickerHoldersMode:+ """Holder discovery never invents a CUSIP and always states its sample."""++ def test_explicit_cusip_skips_the_mapping_entirely(self) -> None:+ hits = [_fts_hit(file_type="INFORMATION TABLE")]+ searches: List[Dict[str, Any]] = []+ # The deadline window and the late tail are separate date ranges with+ # separate totals; the reported universe is their sum, not either one.+ totals = {"2026-05-15": 9627, "2026-07-29": 240}++ def fake(url: str, params: Dict[str, Any] | None = None) -> _Response:+ if url == iht._FTS_URL:+ searches.append(params or {})+ if (params or {}).get("q") == '"037833100"':+ page = [] if (params or {}).get("from") else hits+ return _Response(payload=_fts_payload(page, total=totals[params["enddt"]]))+ return _router()(url, params)++ with patch.object(iht, "_sec_get", side_effect=fake), patch.object(+ iht, "_cusip_map", side_effect=AssertionError("must not build the CUSIP map")+ ):+ payload = json.loads(+ InstitutionalHoldingsTool().execute(mode="ticker_holders", cusip="037833100", quarter="2026Q1")+ )++ data = payload["data"]+ assert data["cusip_source"] == "caller"+ assert data["holders"][0]["value_usd"] == 75000.0+ assert data["holders"][0]["shares"] == 300.0+ assert data["holders"][0]["held"] is True+ # Discovery is CUSIP-scoped; the per-filer chain lookup that follows is+ # CIK-scoped and must not carry the CUSIP query over.+ cusip_queries = [s for s in searches if s.get("q") == '"037833100"']+ assert cusip_queries and all(s.get("ciks") is None for s in cusip_queries)+ assert any(s.get("ciks") == "0001067983" for s in searches)+ assert data["matching_documents_total"] == 9627 + 240++ def test_unmapped_ticker_refuses_rather_than_guessing(self) -> None:+ with patch.object(iht, "_cusip_map", return_value=({}, ["https://example.invalid/f.zip"])):+ payload = json.loads(InstitutionalHoldingsTool().execute(mode="ticker_holders", ticker="ZZZZ"))++ assert payload["ok"] is False+ assert "No CUSIP was guessed" in payload["error"]+ assert payload["ticker"] == "ZZZZ"++ def test_ticker_resolves_through_fails_to_deliver_data(self) -> None:+ mapping = {"AAPL": ("037833100", "APPLE INC;COM NPV")}++ def fake(url: str, params: Dict[str, Any] | None = None) -> _Response:+ if url == iht._FTS_URL:+ return _Response(payload=_fts_payload([_fts_hit(file_type="INFORMATION TABLE")]))+ return _router()(url, params)++ with patch.object(iht, "_sec_get", side_effect=fake), patch.object(+ iht, "_cusip_map", return_value=(mapping, ["u"])+ ):+ payload = json.loads(+ InstitutionalHoldingsTool().execute(mode="ticker_holders", ticker="aapl", quarter="2026Q1")+ )++ assert payload["data"]["cusip"] == "037833100"+ assert payload["data"]["cusip_source"] == "sec_fails_to_deliver"++ def test_share_class_separators_are_normalized(self) -> None:+ """The fails-to-deliver feed writes Berkshire class B as BRKB, not BRK.B."""+ with patch.object(iht, "_cusip_map", return_value=({"BRKB": ("084670702", "BERKSHIRE B")}, ["u"])):+ assert iht._cusip_for_ticker("BRK.B")[0] == "084670702"+ assert iht._cusip_for_ticker("BRK-B")[0] == "084670702"+ assert iht._cusip_for_ticker("BRKB")[0] == "084670702"++ def test_cover_pages_and_other_quarters_are_filtered_out(self) -> None:+ hits = [+ _fts_hit(file_type="13F-HR"),+ _fts_hit(file_type="INFORMATION TABLE", period_ending="2025-12-31", ciks=["0000000002"]),+ _fts_hit(file_type="INFORMATION TABLE", ciks=["0000000003"]),+ ]++ def fake(url: str, params: Dict[str, Any] | None = None) -> _Response:+ if url == iht._FTS_URL:+ return _Response(payload=_fts_payload(hits))+ return _router()(url, params)++ with patch.object(iht, "_sec_get", side_effect=fake):+ payload = json.loads(+ InstitutionalHoldingsTool().execute(mode="ticker_holders", cusip="037833100", quarter="2026Q1")+ )++ assert payload["data"]["filers_seen"] == 1+ assert [h["cik"] for h in payload["data"]["holders"]] == ["0000000003"]++ def test_one_unreadable_filer_does_not_abort_the_batch(self) -> None:+ hits = [+ _fts_hit(file_type="INFORMATION TABLE", ciks=["0000000001"], adsh="0000000001-26-000001"),+ _fts_hit(file_type="INFORMATION TABLE", ciks=["0000000002"], adsh="0001193125-26-226661"),+ ]++ def fake(url: str, params: Dict[str, Any] | None = None) -> _Response:+ if url == iht._FTS_URL:+ wanted = (params or {}).get("ciks")+ page = [h for h in hits if not wanted or wanted in h["_source"]["ciks"]]+ return _Response(payload=_fts_payload(page if not (params or {}).get("from") else []))+ if "000000000126000001" in url:+ raise OSError("connection reset")+ return _router()(url, params)++ with patch.object(iht, "_sec_get", side_effect=fake):+ payload = json.loads(+ InstitutionalHoldingsTool().execute(mode="ticker_holders", cusip="037833100", quarter="2026Q1")+ )++ holders = {h["cik"]: h for h in payload["data"]["holders"]}+ assert payload["ok"] is True+ assert holders["0000000002"]["value_usd"] == 75000.0+ assert "connection reset" in holders["0000000001"]["error"]+ # The broken filer must sort last, never above a real position.+ assert payload["data"]["holders"][0]["cik"] == "0000000002"++ def test_a_restatement_that_dropped_the_security_reports_held_false(self) -> None:+ """The filer surfaced because its original named the CUSIP; the restatement did not."""+ chain = [+ {+ "adsh": "0001193125-26-226661",+ "form": "13F-HR",+ "file_date": "2026-05-15",+ "cover": _PRIMARY_DOC,+ "rows": _DZ_ORIGINAL_ROWS,+ },+ {+ "adsh": "0000905148-26-002029",+ "form": "13F-HR/A",+ "file_date": "2026-06-04",+ "cover": _amended_cover("RESTATEMENT", number=1),+ "rows": _DZ_RESTATED_ROWS,+ },+ ]+ router = _chain_router(chain)++ def fake(url: str, params: Dict[str, Any] | None = None) -> _Response:+ if url == iht._FTS_URL and (params or {}).get("q"):+ hit = _fts_hit(file_type="INFORMATION TABLE", adsh=chain[0]["adsh"])+ return _Response(payload=_fts_payload([] if (params or {}).get("from") else [hit]))+ return router(url, params)++ with patch.object(iht, "_sec_get", side_effect=fake):+ payload = json.loads(+ # TESLA is in the original table and gone from the restatement.+ InstitutionalHoldingsTool().execute(+ mode="ticker_holders", cusip="88160R101", quarter="2026Q1"+ )+ )++ holder = payload["data"]["holders"][0]+ assert holder["held"] is False+ assert holder["value_usd"] is None+ assert holder["error"] is None, "no longer holding it is not a failure to read it"+ assert holder["standing_accession"] == "0000905148-26-002029"+ assert holder["amendment_resolution"]["resolution"] == "restated"+ assert holder["amendment_scope"] == "filer_index"++ def test_a_capped_total_is_labelled_as_a_lower_bound(self) -> None:+ def fake(url: str, params: Dict[str, Any] | None = None) -> _Response:+ if url == iht._FTS_URL:+ hits = [_fts_hit(file_type="INFORMATION TABLE")]+ return _Response(payload=_fts_payload(hits, total=10000, relation="gte"))+ return _router()(url, params)++ with patch.object(iht, "_sec_get", side_effect=fake):+ payload = json.loads(+ InstitutionalHoldingsTool().execute(mode="ticker_holders", cusip="037833100", quarter="2026Q1")+ )++ assert payload["data"]["matching_documents_total_is_lower_bound"] is True++ def test_ticker_and_cusip_both_missing_is_an_error(self) -> None:+ payload = json.loads(InstitutionalHoldingsTool().execute(mode="ticker_holders"))+ assert payload["ok"] is False+ assert "'ticker' or 'cusip'" in payload["error"]+++class TestTopManagersMode:+ """Ranking is bounded, deduplicated and explicitly scoped to its sample."""++ def test_managers_are_ranked_by_reported_portfolio_value(self) -> None:+ hits = [+ _fts_hit(ciks=["0000000001"], adsh="0000000001-26-000001"),+ _fts_hit(ciks=["0000000002"], adsh="0000000002-26-000001"),+ _fts_hit(ciks=["0000000001"], adsh="0000000001-26-000002"),+ ]+ totals = {+ "0000000001-26-000001": 5_000_000_000,+ "0000000001-26-000002": 6_000_000_000,+ "0000000002-26-000001": 900_000_000_000,+ }++ def fake(url: str, params: Dict[str, Any] | None = None) -> _Response:+ if url == iht._FTS_URL:+ wanted = (params or {}).get("ciks")+ page = [h for h in hits if not wanted or wanted in h["_source"]["ciks"]]+ if (params or {}).get("from"):+ page = []+ return _Response(payload=_fts_payload(page, total=12000, relation="gte"))+ accession = url.split("/")[-2]+ for raw, value in totals.items():+ if raw.replace("-", "") == accession:+ return _Response(content=_cover_page(value))+ raise AssertionError(url)++ with patch.object(iht, "_sec_get", side_effect=fake):+ payload = json.loads(InstitutionalHoldingsTool().execute(mode="top_managers", quarter="2026Q1"))++ data = payload["data"]+ assert data["scanned"] == 2, "the same CIK filing twice must be scanned once"+ assert [m["cik"] for m in data["managers"]] == ["0000000002", "0000000001"]+ assert data["managers"][0]["portfolio_value_usd"] == 900_000_000_000.0+ assert data["managers"][0]["value_units_basis"] == "filing_date"+ assert data["universe_documents_is_lower_bound"] is True+ assert "NOT a league table" in data["ranking_note"]+ # Two filings for CIK 1, neither marked as an amendment: the second has+ # no stated relationship to the first, so it is not silently summed in.+ ranked = {m["cik"]: m for m in data["managers"]}+ assert ranked["0000000001"]["portfolio_value_usd"] == 5_000_000_000.0+ assert ranked["0000000001"]["amendment_resolution"]["unresolved_duplicate_originals"] == 1+ assert ranked["0000000001"]["amendment_scope"] == "filer_index"+ assert ranked["0000000002"]["amendment_scope"] == "discovery_window"++ def test_scan_limit_bounds_the_number_of_filings_read(self) -> None:+ hits = [_fts_hit(ciks=[f"000000000{i}"], adsh=f"000000000{i}-26-000001") for i in range(1, 6)]+ fetched: List[str] = []++ def fake(url: str, params: Dict[str, Any] | None = None) -> _Response:+ if url == iht._FTS_URL:+ return _Response(payload=_fts_payload(hits))+ fetched.append(url)+ return _Response(content=_BIG_PRIMARY_DOC)++ with patch.object(iht, "_sec_get", side_effect=fake):+ payload = json.loads(+ InstitutionalHoldingsTool().execute(mode="top_managers", quarter="2026Q1", scan_limit=2)+ )++ assert len(fetched) == 2+ assert payload["data"]["scanned"] == 2++ def test_a_broken_cover_page_is_reported_not_ranked_first(self) -> None:+ hits = [+ _fts_hit(ciks=["0000000001"], adsh="0000000001-26-000001"),+ _fts_hit(ciks=["0000000002"], adsh="0000000002-26-000001"),+ ]++ def fake(url: str, params: Dict[str, Any] | None = None) -> _Response:+ if url == iht._FTS_URL:+ return _Response(payload=_fts_payload(hits))+ if "000000000126000001" in url:+ return _Response(content=b"not xml at all")+ return _Response(content=_BIG_PRIMARY_DOC)++ with patch.object(iht, "_sec_get", side_effect=fake):+ payload = json.loads(InstitutionalHoldingsTool().execute(mode="top_managers", quarter="2026Q1"))++ managers = payload["data"]["managers"]+ assert managers[0]["cik"] == "0000000002"+ assert managers[-1]["error"]+ assert managers[-1]["portfolio_value_usd"] is None++ def test_a_sub_threshold_cover_page_is_rescaled_from_the_information_table(self) -> None:+ """A filer still reporting thousands must not be shrunk 1000x and mis-ranked.++ Modelled on CapitalatWork S.A. (CIK 0002135126), whose 2026-03-31 cover+ page reports 3,361,057. Read as whole dollars that is $3.4M — below the+ $100M that compels filing at all — while its information table prices+ Apple at a believable per-share figure and settles the unit at+ thousands, i.e. a $3.4B portfolio.+ """+ thousands_rows = [+ {"issuer": "APPLE INC", "cusip": "037833100", "value": 16323, "shares": 64258},+ {"issuer": "COCA COLA CO", "cusip": "191216100", "value": 12000, "shares": 157790},+ {"issuer": "VISA INC", "cusip": "92826C839", "value": 9000, "shares": 25641},+ {"issuer": "NVIDIA CORP", "cusip": "67066G104", "value": 4000, "shares": 22936},+ {"issuer": "SPDR S&P 500", "cusip": "78462F103", "value": 2000, "shares": 3610},+ {"issuer": "TESLA INC", "cusip": "88160R101", "value": 1000, "shares": 2500},+ ]++ def fake(url: str, params: Dict[str, Any] | None = None) -> _Response:+ if url == iht._FTS_URL:+ return _Response(payload=_fts_payload([_fts_hit(ciks=["0002135126"])]))+ if url.endswith("primary_doc.xml"):+ return _Response(content=_cover_page(3_361_057))+ if url.endswith("/index.json"):+ return _Response(payload=_INDEX_JSON)+ return _Response(content=_info_table(thousands_rows))++ with patch.object(iht, "_sec_get", side_effect=fake):+ payload = json.loads(InstitutionalHoldingsTool().execute(mode="top_managers", quarter="2026Q1"))++ manager = payload["data"]["managers"][0]+ assert manager["value_units"] == "usd_thousands"+ assert manager["value_units_basis"] == "information_table_implied_price"+ assert manager["portfolio_value_usd"] == 3_361_057_000.0++ def test_an_unreadable_table_leaves_the_prior_intact_and_says_so(self) -> None:+ """When the check cannot run, the number must not silently change."""++ def fake(url: str, params: Dict[str, Any] | None = None) -> _Response:+ if url == iht._FTS_URL:+ return _Response(payload=_fts_payload([_fts_hit()]))+ if url.endswith("primary_doc.xml"):+ return _Response(content=_cover_page(3_361_057))+ raise OSError("index unavailable")++ with patch.object(iht, "_sec_get", side_effect=fake):+ payload = json.loads(InstitutionalHoldingsTool().execute(mode="top_managers", quarter="2026Q1"))++ manager = payload["data"]["managers"][0]+ assert manager["portfolio_value_usd"] == 3_361_057.0+ assert manager["value_units_basis"].startswith("filing_date (unverified:")++ def test_a_new_holdings_fragment_is_not_ranked_as_the_whole_portfolio(self) -> None:+ """DZ BANK's NEW HOLDINGS amendment reports $1.17M against a $41.68B quarter."""+ chain = [+ {+ "adsh": "0001567619-20-019792",+ "form": "13F-HR",+ "file_date": "2026-05-15",+ "cover": _cover_page(41_697_805_000),+ "rows": _DZ_ORIGINAL_ROWS,+ },+ {+ "adsh": "0000905148-26-002030",+ "form": "13F-HR/A",+ "file_date": "2026-05-20",+ "cover": _amended_cover("NEW HOLDINGS", number=1, total=1_165_899_000),+ "rows": _DZ_NEW_HOLDINGS_ROWS,+ },+ ]+ router = _chain_router(chain)+ # Discovery lands on the amendment, which is the trap: read alone it+ # ranks a $41.7B manager as a $1.2M one.+ discovered = _fts_hit(adsh=chain[1]["adsh"], form="13F-HR/A", file_date=chain[1]["file_date"])++ def fake(url: str, params: Dict[str, Any] | None = None) -> _Response:+ if url == iht._FTS_URL and not (params or {}).get("ciks"):+ return _Response(payload=_fts_payload([] if (params or {}).get("from") else [discovered]))+ return router(url, params)++ with patch.object(iht, "_sec_get", side_effect=fake):+ payload = json.loads(InstitutionalHoldingsTool().execute(mode="top_managers", quarter="2026Q1"))++ manager = payload["data"]["managers"][0]+ assert manager["portfolio_value_usd"] == 41_697_805_000.0 + 1_165_899_000.0+ assert manager["amendment_resolution"]["resolution"] == "original_plus_new_holdings"+ assert manager["amendment_resolution"]["standing_accession"] == "0001567619-20-019792"+ assert manager["amendment_scope"] == "filer_index"+ assert "merged_1_new_holdings_amendment" in manager["value_units_basis"]++ def test_a_plain_filing_costs_exactly_one_cover_page_request(self) -> None:+ """The filer index is only pulled when this quarter is actually amended."""+ fetched: List[str] = []++ def fake(url: str, params: Dict[str, Any] | None = None) -> _Response:+ if url == iht._FTS_URL:+ if (params or {}).get("ciks"):+ raise AssertionError("must not pull the filer index for an unamended quarter")+ page = [] if (params or {}).get("from") else [_fts_hit()]+ return _Response(payload=_fts_payload(page))+ fetched.append(url)+ return _Response(content=_BIG_PRIMARY_DOC)++ with patch.object(iht, "_sec_get", side_effect=fake):+ payload = json.loads(InstitutionalHoldingsTool().execute(mode="top_managers", quarter="2026Q1"))++ assert len(fetched) == 1+ assert payload["data"]["managers"][0]["amendment_scope"] == "discovery_window"++ def test_cover_page_units_follow_the_filing_date(self) -> None:+ assert iht._summary_value_usd({"total_value_raw": 296_096_640.0}, "2022-11-14") == 296_096_640_000.0+ assert iht._summary_value_usd({"total_value_raw": 299_007_622_119.0}, "2023-02-14") == 299_007_622_119.0+ assert iht._summary_value_usd({}, "2026-05-15") is None+++class TestDiscoveryWindows:+ """Filer discovery is windowed on the statutory deadline, not a flat lag."""++ def test_the_first_window_is_the_filing_season_itself(self) -> None:+ windows = iht._discovery_windows("2026-03-31")+ assert windows[0] == ("2026-04-01", "2026-05-15"), "quarter end through the 45-day deadline"++ def test_the_windows_tile_the_old_flat_lag_exactly(self) -> None:+ """Splitting the span must not make any filing unreachable."""+ windows = iht._discovery_windows("2026-03-31")+ assert windows[0][0] == "2026-04-01"+ assert windows[-1][1] == "2026-07-29", "still 120 days out, as before the split"+ for earlier, later in zip(windows, windows[1:]):+ assert date.fromisoformat(later[0]) - date.fromisoformat(earlier[1]) == timedelta(days=1)++ def test_the_late_tail_is_only_paged_when_the_sample_is_short(self) -> None:+ queried: List[str] = []++ def fake(url: str, params: Dict[str, Any] | None = None) -> _Response:+ if url == iht._FTS_URL:+ if (params or {}).get("ciks"):+ return _Response(payload=_fts_payload([]))+ queried.append(params["enddt"])+ page = [] if (params or {}).get("from") else [+ _fts_hit(ciks=[f"000000{i:04d}"], adsh=f"000000{i:04d}-26-000001") for i in range(3)+ ]+ return _Response(payload=_fts_payload(page))+ return _Response(content=_BIG_PRIMARY_DOC)++ with patch.object(iht, "_sec_get", side_effect=fake):+ json.loads(+ InstitutionalHoldingsTool().execute(mode="top_managers", quarter="2026Q1", scan_limit=3)+ )+ assert queried == ["2026-05-15"], "the deadline window filled the sample on its own"++ queried.clear()+ with patch.object(iht, "_sec_get", side_effect=fake):+ json.loads(+ InstitutionalHoldingsTool().execute(mode="top_managers", quarter="2026Q1", scan_limit=25)+ )+ assert list(dict.fromkeys(queried)) == [+ "2026-05-15",+ "2026-07-29",+ ], "a short sample reaches into the late tail, and only after exhausting the deadline window"+++class TestInformationTableDiscovery:+ """The right XML is chosen by name and size, not by where EDGAR listed it."""++ def test_a_table_listed_after_the_decoys_is_still_probed_first(self) -> None:+ index = {+ "directory": {+ "item": [+ {"name": "0001193125-26-226661-index.html", "size": ""},+ {"name": "primary_doc.xml", "size": "5555"},+ {"name": "decoy_a.xml", "size": "10"},+ {"name": "decoy_b.xml", "size": "10"},+ {"name": "decoy_c.xml", "size": "10"},+ {"name": "infotable.xml", "size": "45259"},+ ]+ }+ }+ with patch.object(iht, "_sec_get", return_value=_Response(payload=index)):+ urls, note = iht._information_table_urls("0001067983", "0001193125-26-226661")++ assert note is None+ assert urls[0].endswith("infotable.xml"), "fourth in the listing, first in the probe order"+ assert len(urls) == 4++ def test_unnamed_candidates_are_probed_largest_first(self) -> None:+ index = {+ "directory": {+ "item": [+ {"name": "aaa.xml", "size": "10"},+ {"name": "53405.xml", "size": "45259"},+ {"name": "zzz.xml", "size": "900"},+ ]+ }+ }+ with patch.object(iht, "_sec_get", return_value=_Response(payload=index)):+ urls, _ = iht._information_table_urls("0001067983", "0001193125-26-226661")++ assert [u.rsplit("/", 1)[-1] for u in urls] == ["53405.xml", "zzz.xml", "aaa.xml"]++ def test_the_candidate_list_stays_bounded(self) -> None:+ index = {"directory": {"item": [{"name": f"x{i}.xml", "size": "10"} for i in range(40)]}}+ with patch.object(iht, "_sec_get", return_value=_Response(payload=index)):+ urls, _ = iht._information_table_urls("0001067983", "0001193125-26-226661")++ assert len(urls) == iht._MAX_TABLE_CANDIDATES+++class TestCusipMapping:+ """The fails-to-deliver archive is parsed, never fabricated."""++ def test_archive_is_parsed_into_a_ticker_map(self) -> None:+ payload = (+ "SETTLEMENT DATE|CUSIP|SYMBOL|QUANTITY (FAILS)|DESCRIPTION|PRICE\n"+ "20260701|037833100|AAPL|85824|APPLE INC;COM NPV|294.38\n"+ "20260701|084670702|BRKB|11995|BERKSHIRE HATHWY INC(HLDG CO)B|500.39\n"+ "20260702|037833100|AAPL|14619|APPLE INC;COM NPV|308.63\n"+ "malformed row\n"+ "\n"+ )+ buffer = io.BytesIO()+ with zipfile.ZipFile(buffer, "w") as archive:+ archive.writestr("cnsfails202607a.txt", payload)++ mapping: Dict[str, Any] = {}+ with patch.object(iht, "_sec_get", return_value=_Response(content=buffer.getvalue())):+ iht._load_ftd_map("https://example.invalid/cnsfails202607a.zip", mapping)++ assert mapping["AAPL"] == ("037833100", "APPLE INC;COM NPV")+ assert mapping["BRKB"] == ("084670702", "BERKSHIRE HATHWY INC(HLDG CO)B")+ assert len(mapping) == 2, "a blank or malformed row must be skipped, not crash the build"++ def test_newest_archive_is_discovered_from_the_foia_index(self) -> None:+ html = (+ '<a href="/files/data/fails-deliver-data/cnsfails202605a.zip">x</a>'+ '<a href="/files/data/other/fails-deliver-data/cnsfails202607a.zip">y</a>'+ '<a href="/files/data/fails-deliver-data/cnsfails202606b.zip">z</a>'+ )+ with patch.object(iht, "_sec_get", return_value=_Response(text=html)):+ urls = iht._ftd_urls()++ assert urls == ["https://www.sec.gov/files/data/other/fails-deliver-data/cnsfails202607a.zip"]++ def test_pinned_url_wins_and_costs_no_request(self) -> None:+ with patch.dict("os.environ", {iht._ENV_FTD_URL: "https://example.invalid/pinned.zip"}), patch.object(+ iht, "_sec_get", side_effect=AssertionError("must not fetch the index")+ ):+ assert iht._ftd_urls() == ["https://example.invalid/pinned.zip"]+++class TestToolContract:+ """The BaseTool contract the registry relies on."""++ def test_schema_and_flags(self) -> None:+ tool = InstitutionalHoldingsTool()+ assert tool.name == "get_institutional_holdings"+ assert tool.is_readonly is True+ assert InstitutionalHoldingsTool.check_available() is True+ schema = tool.to_openai_schema()["function"]+ assert set(schema["parameters"]["properties"]) == {+ "mode", "manager", "ticker", "cusip", "quarter", "include_changes", "top_n", "scan_limit", "offset",+ }+ assert schema["parameters"]["properties"]["mode"]["enum"] == list(iht._MODES)++ def test_the_tool_is_auto_discovered(self) -> None:+ from src.tools import build_registry++ assert "get_institutional_holdings" in build_registry()++ @pytest.mark.parametrize(+ ("kwargs", "fragment"),+ [+ ({"mode": "nope"}, "mode must be one of"),+ ({"mode": "manager_holdings"}, "requires 'manager'"),+ ({"manager": "X", "offset": "abc"}, "offset must be an integer"),+ ],+ )+ def test_bad_arguments_return_envelopes_not_exceptions(self, kwargs: Dict[str, Any], fragment: str) -> None:+ payload = json.loads(InstitutionalHoldingsTool().execute(**kwargs))+ assert payload["ok"] is False+ assert fragment in payload["error"]++ def test_full_text_search_error_document_is_not_read_as_success(self) -> None:+ """EDGAR answers HTTP 200 with an error body, so a missing hits block must raise."""+ error_doc = {"errorType": "ResponseError", "errorMessage": "boom"}+ with pytest.raises(RuntimeError, match="boom"), patch.object(+ iht, "_sec_get", return_value=_Response(payload=error_doc)+ ):+ iht._fts({"forms": "13F-HR"})++ def test_a_search_error_falls_back_instead_of_reporting_an_empty_portfolio(self) -> None:+ """Losing full-text search must degrade to the submissions index, not to zero filings."""+ submissions = {+ "filings": {+ "recent": {+ "form": ["13F-HR"],+ "accessionNumber": ["0001193125-26-226661"],+ "filingDate": ["2026-05-15"],+ "reportDate": ["2026-03-31"],+ }+ }+ }++ def fake(url: str, params: Dict[str, Any] | None = None) -> _Response:+ if url == iht._FTS_URL:+ return _Response(payload={"errorMessage": "boom"})+ return _router()(url, params)++ with patch.object(iht, "_sec_get", side_effect=fake), patch.object(+ iht, "get_submissions", return_value=submissions+ ):+ payload = json.loads(InstitutionalHoldingsTool().execute(manager="0001067983", quarter="2026Q1"))++ assert payload["ok"] is True+ assert payload["data"]["position_count"] == 5++ def test_both_filing_indexes_failing_surfaces_the_error(self) -> None:+ with patch.object(iht, "_sec_get", side_effect=OSError("sec.gov unreachable")), patch.object(+ iht, "get_submissions", side_effect=OSError("sec.gov unreachable")+ ):+ payload = json.loads(InstitutionalHoldingsTool().execute(manager="0001067983"))+ assert payload["ok"] is False+ assert "sec.gov unreachable" in payload["error"]++ def test_result_stays_inside_the_tool_result_budget(self) -> None:+ """A 400-position portfolio must page, not overflow and lose its tail silently."""+ rows = [+ {"issuer": f"ISSUER NUMBER {i:04d} INCORPORATED", "cusip": f"{i:09d}", "value": 10_000 - i, "shares": 50}+ for i in range(400)+ ]+ with patch.object(iht, "_sec_get", side_effect=_router(current=_info_table(rows))):+ out = InstitutionalHoldingsTool().execute(manager="0001067983", quarter="2026Q1", top_n=100)++ payload = json.loads(out)+ assert len(out) <= TOOL_RESULT_LIMIT+ assert payload["data"]["position_count"] == 400+ assert payload["paging"]["returned"] < 100+ assert payload["paging"]["complete"] is False+ assert payload["paging"]["next_offset"] == payload["paging"]["returned"]++ def test_requests_share_the_sec_throttle_bucket(self) -> None:+ """Every call must be spaced under the same bucket as the other SEC callers."""+ captured: Dict[str, Any] = {}++ def fake_throttled_get(url: str, **kwargs: Any) -> Any:+ captured.update(kwargs)+ captured["url"] = url+ raise OSError("stop here")++ with patch.object(iht, "throttled_get", side_effect=fake_throttled_get):+ with pytest.raises(OSError):+ iht._sec_get("https://data.sec.gov/x")++ assert captured["host_key"] == "sec"+ assert captured["min_interval"] > 0+ assert "User-Agent" in captured["headers"]