# Intel agent failed fetching SEC filings mid morning briefing

## TL;DR
When the filing fetch dies mid briefing, the briefing dies with it unless the agent treats fetching as unreliable by design. The fix is a fetch layer with retries, a local filing cache so already-downloaded accessions never re-download, and a briefing that degrades to cached data instead of failing. Checkpoint each fetch so a rerun resumes where it stopped.

## The error
```text
(agent run failed)
intel agent failed fetching SEC filings mid morning briefing; run aborted with no briefing output
```

## When this helps
- a briefing agent dies on filing fetch errors
- morning briefings fail intermittently on EDGAR
- building resilient filing intake for agents
- designing rerun-safe agent pipelines

## When it doesn't
- EDGAR itself is down; then every fetch fails and the briefing waits
- the fetch code has a bug; retries do not fix wrong URLs
- you need real-time filings; caching trades freshness for reliability

## Works with
python 3.8+ with requests. EDGAR endpoints as of 2026.

## Steps
### 1. Wrap every filing fetch in retries with backoff
```python
import time, requests
s = requests.Session()
s.headers.update({"User-Agent": "IntelBriefingBot/1.0"})
def fetch(url):
    for i in range(4):
        r = s.get(url, timeout=30)
        if r.status_code == 200:
            return r.content
        time.sleep(5 * (i + 1))
    raise RuntimeError("fetch failed after retries: " + url)
print("fetch wrapper ready")
```
Expected: A fetch function that survives transient EDGAR hiccups. Four tries with growing waits covers most blips.

### 2. Cache filings by accession so reruns resume
```python
import hashlib, os
os.makedirs("filings", exist_ok=True)
def cached_fetch(url):
    fp = "filings/" + hashlib.md5(url.encode()).hexdigest()
    if os.path.exists(fp):
        return open(fp, "rb").read()
    data = fetch(url)
    open(fp, "wb").write(data)
    return data
print("cache wrapper ready; reruns skip downloaded filings")
```
Expected: A cache that makes reruns idempotent. The morning briefing resumes instead of restarting from zero.

### 3. Checkpoint progress after each filing
```python
import json
state = {"done": ["[acc1]", "[acc2]"], "pending": ["[acc3]"]}
open("briefing_state.json", "w").write(json.dumps(state))
print("checkpoint:", open("briefing_state.json").read())
```
Expected: A state file. If the run dies, the next run reads the checkpoint and continues with the pending list.

### 4. Degrade to cached data instead of failing the briefing
```python
import json
state = json.load(open("briefing_state.json"))
if state["pending"]:
    print("warning: briefing built from", len(state["done"]), "cached filings;", len(state["pending"]), "pending")
print("briefing proceeds with a staleness note rather than aborting")
```
Expected: A briefing that always ships. Missing filings become a disclosed gap, not a crashed run.

## Other ways people phrase this
### agent failed downloading sec filings
The fetch layer needs retries, cache, and checkpoints. All three, not one.

### morning briefing failed edgar fetch
Briefing-time EDGAR load causes transient failures. The cache absorbs them.

### briefing agent no output fetch error
Failing the whole briefing on one fetch is the design bug. Degrade instead.

## Why it happens
Agents that fetch-then-brief in one linear pass have a single point of failure: any fetch error kills the run. EDGAR has transient failures, especially during briefing hours. The resilient pattern separates fetching from briefing with a cache and checkpoints between them, so failures are recoverable and partial data still briefs.

## Edge cases
- Checkpoint writes must be atomic; write to a temp file and rename.
- Cache invalidation matters around restatements; re-check amendments on a schedule.
- A fetch that 403s permanently should be marked failed, not retried forever.
- Log which filings were cached versus fresh so the briefing can note staleness.

## Provenance

Resolved from the public thread: https://vectle.com/posts/pst_dpgiLmBTdgsZ8QPC_sKO4g
