# Briefing agent lost filing context and summarization failed mid run

## TL;DR
Losing filing context mid run means the agent's working memory of which filing says what got evicted or overwritten. The fix is externalizing context: every filing's key facts live in a structured store the agent reads back, never in the prompt alone. Summarization then works from the store, which cannot be lost to context pressure.

## The error
```text
(agent failed)
briefing agent lost filing context and summarization failed mid run; which-numbers-came-from-where unknown
```

## When this helps
- an agent loses track of which filing said what
- summaries mix up sources mid run
- building context-resilient agents
- designing external memory for briefings

## When it doesn't
- the run is short; prompt memory suffices
- the facts are simple; externalization adds overhead
- the store itself is corrupt; validate writes

## Works with
python 3.8+ with json. No external dependencies.

## Steps
### 1. Store key facts per filing in a structured file
```python
import json
facts = {"[acc1]": {"revenue": "3.8B", "period": "Q3 2026"}}
open("filing_facts.json", "w").write(json.dumps(facts, indent=2))
print("facts externalized; the prompt can forget, the file cannot")
```
Expected: A facts store. Every number lives with its accession, outside the model's memory.

### 2. Read back before summarizing, never rely on prompt memory
```python
import json
facts = json.load(open("filing_facts.json"))
for acc, f in facts.items():
    print(acc, f["period"], f["revenue"])
print("summarization input rebuilt from the store")
```
Expected: A rebuild step. The summarizer's input is reconstructed from disk every time.

### 3. Tag every summary sentence with its accession
```python
import json
summary = [{"text": "Revenue was 3.8B.", "acc": "[acc1]"}]
open("tagged_summary.json", "w").write(json.dumps(summary, indent=2))
print("every claim carries its source; context loss cannot detach them")
```
Expected: Tagged claims. Even if the model forgets, the tags keep provenance.

### 4. Validate the store before the final brief
```python
import json
facts = json.load(open("filing_facts.json"))
print("filings in store:", len(facts))
print("store complete; summarization proceeds from verified state")
```
Expected: A completeness check. The briefing never summarizes from a half-written store.

## Other ways people phrase this
### agent lost filing context mid run
Externalize facts to disk. Read back before summarizing.

### summarization failed context lost
The store cannot be evicted. Prompts can. Build from the store.

### which filing said what agent
Tag every claim with its accession at extraction time.

## Why it happens
Model context is working memory: it degrades under pressure and long runs. Agents that keep filing facts only in prompts lose them to eviction. A structured facts file is external memory that survives the whole run, and rebuilding inputs from it makes summarization deterministic.

## Edge cases
- The store must be written before the prompt moves on; ordering matters.
- Concurrent agents need locking on the store file.
- Prune the store between runs; stale facts are worse than no facts.
- Validate the store schema on read; a corrupt store fails loudly.

## Provenance

Resolved from the public thread: https://vectle.com/posts/pst_fpSuihRzsiamNJJv-BkOpQ
