feat(knowledge): real Wikipedia vertical with containment-checked KG

knowledge/: wiki.py (official MediaWiki API, own session+UA; fetch_rich appends wikitext tables as text - plain extracts drop them), chunker.py (section-aware, level paths), checks.py (verbatim containment, quote-aware), store.py (SQLite append-only, 4 contradiction cases, probe by entity AND relation with stemming), extract.py (dspy Signatures: TRANSLATE resolves pronouns from context and emits queries+entities, EXTRACT per chunk, ANSWER from KG rows), expert.py (the pipeline: KG-first probe -> gap -> translate -> search -> fetch_rich -> chunk -> gate -> extract -> check -> merge -> re-probe -> answer). Wiring: KnowledgeEngine in floop/engine.py (machine injects charge+modules at call time; baseline fallback on pipeline failure), machine routes knowledge questions to it, config.yaml knowledge: block. E2E verified live: Loire length answered from KG with zero network; the 2-hop EU-river question resolved France (population table row quote) -> Loire (KG hit) in 12 calls/75s. Six live-run bugs closed (wiki lib 403/HTML, dropped tables, pipes-in-quotes parse, gate stemming, relation probe, pronoun context). RUNLOG Run C added; code/README honest map updated. Tests 17/17.
This commit is contained in:
2026-10-16 23:19:09 +02:00
parent aecaac01eb
commit 9094b2aec0
15 changed files with 1111 additions and 13 deletions
+60 -1
View File
@@ -186,4 +186,63 @@ are stubs — so everything is honestly `Unverified`. LM: local `qwen3.8:27b` (h
3. dspy 3.4 + litellm: model ids need the `openai/` prefix for OpenAI-compatible endpoints;
the class is `dspy.LM` (not `dspy.LLM`). Wrapped once in `floop/llm.py`.
4. Budget must charge engine LLM calls too (the engine IS an LLM call in the draft) —
fixed after Run A showed engines riding for free.
fixed after Run A showed engines riding for free.
---
# Run C — the knowledge vertical, live (2026-10-17)
**Goal:** give the `knowledge` expert a real engine (KNOWLEDGE.md): KG-first probe ->
Wikipedia gather -> containment-checked extraction -> answer from quoted rows.
Wikipedia (en) only, per decision 2026-10-17; DuckDuckGo/general-web later.
## C1 — Loire length (cold then warm)
- Cold run: search "loire river" -> fetch Loire (40,229 chars) -> 28 section chunks ->
3 gated -> 1 fact extracted: `loire river | length | 1,006 kilometres` with verbatim
quote, containment-checked against the full article text. 4/4 candidates passed.
- Warm run (KG already holds it): **"1,006 kilometres" answered from the graph with zero
network calls**, source=knowledge_graph, quote verbatim. The KG-first law pays.
## C2 — "Longest river of the second most populated EU state" (the 2-hop question)
This one took six real bugs to close, each found by a live run:
1. **Wikipedia pip lib unusable** — default User-Agent 403; empty-string params make the
API return HTML. Fix: official MediaWiki action API with own session (`wiki.py`).
2. **Plain extracts drop tables** — the population ranking article is all table; the
extract was 393 chars. Fix: `fetch_rich` appends wikitext tables parsed to rows.
3. **Pipes inside quotes** — the extractor quoted table rows ("France | 68,635,900 | 15.2%");
`parse_facts` split on every pipe and the quote collapsed to "France" (<10 chars, rejected).
Fix: `split("|", 3)` — the quote is the last field and may contain pipes.
4. **Gate too strict** — "populated" never matched "population" in chunks. Fix: stemming
in the gate and in the probe (`store.stem`).
5. **Probe matched only entity names** — ranking questions have unknown answer entities;
the probe now also matches relations and values.
6. **Pronouns reached the pipeline naked** — "What is the longest river located in THAT
STATE?" translated to "United States" river lists (50 wasted extraction calls in one
dead run). Fix: the machine passes parent question + answered evidence rows into the
pipeline; TRANSLATE resolves references; the initial probe seeds include the resolved
entities (E1 "France" -> probe finds `the loire | is the longest river in | France`).
Final run (2026-10-17 23:17): Q1 France (E1, quote = population table row) -> Q2 "that
state" resolved to France -> **KG hit: The Loire River (E2)** -> answer cites both rows.
12 LLM calls, 75s, zero network on Q2 (all facts already in the graph).
## Honesty notes
- Extraction quality bounds KG quality: containment guarantees fidelity to the source,
not source truth.
- The gather still spends calls on tangential articles (US river lists matched the
"longest river" gate); tuning pending (README next-steps #5).
- KG carries noise from the debugging runs (e.g. `the loire` vs `loire river` nodes);
entity-resolution policy pending by design (conservative draft).
- `verified=False` still: the prover is a stub. But knowledge rows now carry
`source-verified: verbatim containment in trusted doc` — a real, deterministic check.
## Environment notes
- Run C2 used `uv run` (pyproject fixed earlier: name floop, hatchling, packages=["floop"]).
- dspy 3.4 `dspy.LM(...)` returns a list of AttributeDicts on raw call (`text` key) —
wrapped in `floop/llm.py:active_lm`.
- Tests at this point: 9 state + 5 machine + 3 knowledge = 17/17.