Skip to content

feat(extract): LLM-powered fact enrichment + auto-classification (#218, #219) - #224

Merged
hurttlocker merged 7 commits into
mainfrom
feat/smarter-extraction
Feb 24, 2026
Merged

hurttlocker merged 7 commits into
mainfrom
feat/smarter-extraction

Conversation

@hurttlocker

Copy link
Copy Markdown
Owner

Summary

Adds --enrich flag to the import, extract, and connect sync pipelines. When combined with --llm, sends rule-extracted facts + raw text to an LLM to discover what the rules missed.

Closes #218

What the LLM finds

  1. Decision reasoning — WHY was something decided? ("locked ORB config" → because IEX volume filter was blocking trades)
  2. Implicit relationships — Connections not stated as "X is Y" ("SB needs this for Eyes Web" → SB ↔ Eyes Web ↔ health)
  3. Confidence calibration — Tentative statements get lower confidence ("we might try X" → 0.4) vs definitive ("X is locked" → 0.9)
  4. Missed facts — Important information the rules skipped entirely

Design Decisions

  • Additive only — never removes or modifies rule-extracted facts
  • Fuzzy dedup — compares LLM facts against rules (case-insensitive, substring containment)
  • Graceful fallback — LLM errors → rule-only output (no data loss)
  • Tagged llm-enrich — distinct from rules and llm extraction methods
  • 10s timeout — per enrichment call
  • Markdown fence stripping — handles \``json` wrapped responses
  • --enrich implies --extract — no need to specify both

CLI Usage

# Import with enrichment
cortex import notes.md --extract --enrich --llm google/gemini-2.0-flash

# Standalone extract with enrichment
cortex extract file.md --enrich --llm openrouter/openai/gpt-5.1-codex-mini

# Connector sync with enrichment
cortex connect sync --provider file --extract --enrich --llm google/gemini-2.0-flash

Files Changed

File Lines Description
internal/extract/enrich.go +295 Core enrichment logic, prompt building, response parsing, dedup
internal/extract/enrich_test.go +652 21 tests covering all paths
cmd/cortex/main.go +160 Wire --enrich into import, extract, connect sync
internal/connect/sync.go +4 Add Enrich field to SyncOptions

Tests

  • 21 new enrichment tests (all pass)
  • All 15 test packages green

Dependencies

@hurttlocker hurttlocker added v0.9.0 v0.9.0 LLM-Augmented Intelligence llm LLM integration features labels Feb 24, 2026
@hurttlocker hurttlocker changed the title feat(extract): LLM-powered fact enrichment (#218) feat(extract): LLM-powered fact enrichment + auto-classification (#218, #219) Feb 24, 2026
LavonTMCQ and others added 7 commits February 24, 2026 14:51
Add --enrich flag to import, extract, and connect sync commands.
When combined with --llm, sends rule-extracted facts + raw text to an
LLM to discover:
- Decision reasoning (WHY things were decided)
- Implicit relationships (entity connections not stated explicitly)
- Confidence calibration (tentative vs definitive statements)
- Missed facts (things rules couldn't pattern-match)

Key design decisions:
- Additive only: never removes or modifies rule-extracted facts
- Fuzzy dedup: compares LLM facts against rules (case-insensitive,
  substring containment on subject+predicate+object)
- Graceful fallback: LLM errors result in rule-only output
- Extraction method tagged as 'llm-enrich' (distinct from 'rules' and 'llm')
- 10s timeout per enrichment call
- Markdown fence stripping for LLM responses
- Subject normalization + length capping (MaxSubjectLength=50)

New files:
- internal/extract/enrich.go (295 lines)
- internal/extract/enrich_test.go (652 lines, 21 tests)

CLI integration:
- cortex import <path> --extract --enrich --llm google/gemini-2.0-flash
- cortex extract <file> --enrich --llm openrouter/openai/gpt-5.1-codex-mini
- cortex connect sync --provider file --extract --enrich --llm <model>

All 15 test packages pass.
…to 30s

Benchmark results (5 real files, 103 rule-extracted facts baseline):

| Model                          | +Facts | +%    | Avg Latency |
|--------------------------------|--------|-------|-------------|
| google/gemini-2.0-flash        | +18    | +17.5%| 5,978ms     |
| openrouter/openai/gpt-5.1-codex-mini | +0 | +0%  | 9,898ms (timeouts) |

Key findings:
- Gemini 2.0 Flash: reliable, +17.5% new facts, free tier
- Codex Mini: empty responses / timeouts for structured enrichment
- TOOLS.md: +11 enriched facts (22% increase) — relationships, state facts
- 2026-02-20.md: +7 enriched facts (700% increase on 1 rule fact)
- Gemini Flash recommended as default for enrichment

Fixes:
- Skip old tier-2 LLM when --enrich set (prevents retry storms)
- Bump enrichTimeout from 10s to 30s (OpenRouter cold starts)
Add `cortex classify` command that reclassifies facts from the catch-all
'kv' bucket into proper semantic types using batch LLM processing.

New files:
- internal/extract/classify.go (283 lines) — batch classification logic
- internal/extract/classify_test.go (362 lines, 15 tests)
- internal/store/facts.go — UpdateFactType() method
- internal/store/store.go — Store interface addition

CLI:
  cortex classify --llm google/gemini-2.0-flash --dry-run
  cortex classify --llm google/gemini-2.0-flash --limit 100 --batch-size 25
  cortex classify --llm openrouter/openai/gpt-5.1-codex-mini --json

Design:
- Batch processing (default 50 facts/batch, configurable)
- 0.8 confidence threshold prevents low-confidence reclassifications
- Supports config type (not in original extract schema)
- --dry-run shows changes without applying
- Graceful batch error handling (errors don't halt remaining batches)
- Markdown fence stripping for LLM responses

Benchmark (50 kv facts, Gemini 2.0 Flash):
- 8/50 reclassified (16%): 3 temporal, 2 decision, 2 config, 1 preference
- 0 errors, 1 batch, 8.0s latency
- Estimated full corpus: ~28K kv facts × 16% = ~4,480 reclassifications

All 15 test packages pass.
Add `cortex summarize` command that consolidates redundant facts within
topic clusters, creating authoritative summaries and superseding originals.

New files:
- internal/extract/summarize.go (321 lines) — cluster summarization logic
- internal/extract/summarize_test.go (352 lines, 14 tests)

CLI:
  cortex summarize --llm google/gemini-2.0-flash --dry-run
  cortex summarize --llm google/gemini-2.0-flash --cluster 1
  cortex summarize --llm google/gemini-2.0-flash --min-cluster-size 10

Design:
- Leverages v0.8.0 topic clustering infrastructure
- LLM identifies redundant/overlapping facts → consolidates into one
- Superseded originals are marked (not deleted) for audit trail
- 45s timeout per cluster, graceful error handling
- Integrates with store.SupersedeFact for provenance tracking
- Markdown fence stripping for LLM responses

Benchmark (1 cluster, 100 facts, Gemini 2.0 Flash):
- 100 → 80 facts (1.2x compression)
- 42 facts superseded
- 18.7s latency
- Free tier

All 15 test packages pass, +904 lines.
Enrichment default: openrouter/x-ai/grok-4.1-fast
- Only model that reliably finds new facts (+26 across 3 test files)
- All others returned 0 or timed out

Classification default: openrouter/deepseek/deepseek-v3.2
- 38/50 (76%) reclassification rate, 0 errors, 34s
- Best balance of quality, speed, and cost ($0.25/$0.40 per M tokens)

Changes:
- extract --enrich now auto-defaults to Grok 4.1 Fast (no --llm needed)
- classify now auto-defaults to DeepSeek V3.2 (no --llm needed)
- Enrichment timeout: 30s → 3min (OpenRouter cold starts)
- Classification timeout: 30s → 3min
- Classification batch size: 50 → 10 (prevents timeouts on slower models)
- Query expansion stays on Gemini 2.0 Flash (free, hot path, 5s timeout)
- Full benchmark script: scripts/benchmark_v09.sh (6 models tested)

Benchmarked models: Gemini 2.0 Flash, MiniMax M2.5, Gemini 3 Flash Preview,
DeepSeek V3.2, Grok 4.1 Fast, Codex Mini — across enrichment + classification.
MiniMax broken (80% errors), Codex Mini barely functional for structured output.

Refs: #218, #219
@hurttlocker
hurttlocker force-pushed the feat/smarter-extraction branch from e643f70 to 3b78e46 Compare February 24, 2026 19:52
@hurttlocker
hurttlocker merged commit abf718d into main Feb 24, 2026
4 checks passed
@hurttlocker
hurttlocker deleted the feat/smarter-extraction branch March 1, 2026 17:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llm LLM integration features v0.9.0 v0.9.0 LLM-Augmented Intelligence

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🔬 Smarter Fact Extraction (Import Pipeline)

1 participant