Repository navigation
feat(extract): LLM-powered fact enrichment + auto-classification (#218, #219) - #224
Merged
Merged
Conversation
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
force-pushed
the
feat/smarter-extraction
branch
from
February 24, 2026 19:52
e643f70 to
3b78e46
Compare
This was referenced Feb 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
--enrichflag 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
Design Decisions
llm-enrich— distinct fromrulesandllmextraction methods\``json` wrapped responses--enrichimplies--extract— no need to specify bothCLI Usage
Files Changed
internal/extract/enrich.gointernal/extract/enrich_test.gocmd/cortex/main.go--enrichinto import, extract, connect syncinternal/connect/sync.goEnrichfield to SyncOptionsTests
Dependencies
internal/llm/adapter