# Dual-Perspective Background Graph-Builder (PoC 2)

Zero-dependency Python 3 proof of concept demonstrating how a quiet background cartographer ingests streaming spoken turns (`transcript_events.jsonl`) and sparse archive metadata (`artifacts.json`) into a typed, provenance-aware memory graph without collapsing two people's distinct memories into one flat record.

## What this bundle contains

- `graph_builder.py` — standalone Python 3 module + CLI (`--verify` self-test suite, streaming ingestion, time-scoped place resolution, dual-perspective divergence detection, and Expected Information Gain gap ranking).
- `sample_stream.jsonl` — 10 anonymized synthetic spoken turns alternating between **Perspective A (Child)** and **Perspective B (Parent)** across four broad regions (Beijing → rural English village → North London → Docklands), with audio timestamp spans (`t0..t1` in seconds).
- `sample_artifacts.json` — 11 synthetic sparse archive records (undated scans, ephemera, and two Tier 1 EXIF-dated photos) plus 3 initial active archive gaps.
- `memory_graph.json` — deterministic JSON graph generated by running `graph_builder.py` on the sample inputs.

## Quick reproduction

Run the built-in 9-assertion verification suite (requires Python 3.10+, zero external packages):

```bash
python3 graph_builder.py --verify
```

Generate a fresh `memory_graph.json` from the sample stream and sparse artifacts:

```bash
python3 graph_builder.py \
  --stream sample_stream.jsonl \
  --artifacts sample_artifacts.json \
  --out memory_graph.json
```

## Architecture & invariants

1. **Time-scoped entity resolution:** Places are disambiguated by both lexical aliases and era intervals so repeating region names (e.g., *North London terrace, 2004–2007* vs. *North London council flat, 2011–2016*) never collapse into one merged node.
2. **Dual-perspective storage on shared coordinates:** Every place node maintains separate `perspectives.child` and `perspectives.parent` slots holding turn IDs, exact audio ranges (`[t0, t1]`), claimed years, salient concrete motifs, and emotional valence scores.
3. **Provenance hierarchy:**
   - **Tier 1 (`tier1_hardware_exif`)** — hardware EXIF timestamps and rough region tags lock canonical chronology.
   - **Tier 2 (`tier2_contemporaneous_scan`)** — undated prints and ephemera waiting to be anchored.
   - **Tier 3 (`tier3_spoken_recall`)** — retrospective turns carrying `[t0, t1]` audio citations. When a spoken year conflicts with Tier 1 EXIF (e.g., Child recalling `2020` for a Docklands move confirmed by `2021-03-04` EXIF), EXIF sets the canonical timeline while the spoken claim remains preserved and tagged `superseded_by_exif`.
   - **Tier 4 (`tier4_inferred_link`)** — computed perspective divergence markers.
4. **Divergence vs. agreement detection:** On every coordinate carrying both perspectives, the engine distinguishes:
   - `temporal_clash` — conflicting year claims or EXIF contradiction.
   - `salience_split` — identical coordinate/era with opposing lived valence (`|Δvalence| >= 0.75`).
   - `detail_divergence` — low motif overlap with moderate emotional spread.
   - `convergent_anchor` — aligned timeline and emotional tone (`|Δvalence| <= 0.30`).
5. **Risk-adjusted Expected Information Gain (EIG):** Remaining archive gaps are ranked by:

   $$S(g) = \frac{\mathrm{EIG}(g)}{C_{\text{trust}}(g)} = \frac{r \cdot H(p_g) + m_g \cdot h_{\text{art}} + b_g \cdot h_{\text{bridge}}}{1.0 + 1.2 \cdot \mathrm{sensitivity}(g) + 0.8 \cdot (1 - \mathrm{reversibility}(g))}$$

   This surfaces high-information, low-friction bridge voids (like the 2008–2010 interim gap with three orphan scans) ahead of emotionally sensitive gaps (like the late-2001 Beijing departure week).

## Privacy & synthetic scope

All turns, filenames, and region labels in `sample_stream.jsonl` and `sample_artifacts.json` are synthetic and anonymized (broad region labels only; no street numbers, no real personal names, no private photographs).

## Easy extension hooks

- **Swap the extractor:** Replace `extract_details()` and `score_salience()` with structured JSON output from a local or API LLM to handle pronoun coreference and bilingual code-switching while keeping the exact same `DualMemoryGraphBuilder` state and divergence invariants.
- **Test your own timeline:** Copy `sample_artifacts.json` and `sample_stream.jsonl`, replace the broad place nodes and turns, and run `python3 graph_builder.py --stream my_turns.jsonl --artifacts my_meta.json --out out.json`.
