Commit Graph

4 Commits

Author SHA1 Message Date
ruv 17ce6ba67d feat(intelligence): labelled held-out corpus + nDCG metrics (ADR-081, 3.10.21)
Replace the regex-over-subject relevance proxy used in ADRs 077-080 with
a hand-curated labelled corpus. Each of the 10 bench queries gets
expectedSubstrings: string[] — case-insensitive substring matches against
the pattern name. Adds precision@3, nDCG@3, nDCG@5 alongside the existing
hit-rate and MRR metrics. Regex metrics preserved for back-compat.

WHY: The regex proxy was both under- and over-reporting. Honest SOTA
requires honest measurement.

| Config              | Regex top-1 | Label top-1 | Direction         |
|---------------------|------------:|------------:|-------------------|
| Hybrid (3.10.19)    |         80% |        90%  | regex under-reports |
| Hybrid + Rerank     |         90% |        80%  | regex over-reports  |

Labelled metrics across the full 3.10.17 → 3.10.20 push:

| Metric               | Cosine | Hybrid | Hybrid+Rerank |
|----------------------|-------:|-------:|--------------:|
| Label top-1 hit rate |     0% |    90% |           80% |
| Label top-3 hit rate |     0% |    90% |          100% |
| Label MRR@3          |  0.000 |  0.900 |         0.883 |
| Label precision@3    |  0.000 |  0.400 |         0.667 |
| Label nDCG@3         |  0.000 |  0.900 |         0.913 |

The cross-encoder optimises for "find all the right docs" (precision@3
0.40 → 0.67) while hybrid alone optimises for "find THE right doc first"
(top-1 80% → 90%). Default rerank: false remains correct for top-1
callers; opt-in rerank: true for richer top-K consumers.

Implementation:
  - QUERIES gains expectedSubstrings: string[] per item
  - isRelevant(name, expectedSubstrings) — case-insensitive substring match
  - ndcgAtK(rankedRelevance, k) — binary relevance + ideal-DCG normalisation
  - 6 new metrics in summary JSON + console output
  - Old regex metrics kept under "regex proxy" labels for back-compat

Co-Authored-By: RuFlo <ruv@ruv.net>
2026-05-30 14:34:59 -04:00
ruv 57a4a1b735 feat(intelligence): cross-encoder reranker (ADR-080, 3.10.20)
Opt-in cross-encoder rerank pass over the hybrid+BM25 top-K. Uses
Xenova/ms-marco-MiniLM-L-6-v2 (int8, ~30MB) via direct AutoTokenizer +
AutoModelForSequenceClassification (xenova v2 pipeline can't do pair
encoding reliably). Lazy-loaded, one-shot load policy, graceful
degradation when model unavailable.

Architecture: hybrid top-K*6 → cross-encoder scores all pairs →
combined = 0.5*normalise(hybrid) + 0.5*normalise(ce) → top-K. MMR
bypassed when reranking is on (cross-encoder's calibrated score is
more precise than MMR's diversity heuristic at this stage).

Linear combination is essential — ablation showed:
  - Hybrid alone:    8/10 top-1, 8/10 top-3, MRR 0.800
  - CE alone:        6/10 top-1, 10/10 top-3, MRR 0.733
  - Combined 0.5/0.5: 9/10 top-1, 10/10 top-3, MRR 0.933 ← default

Default is OFF (latency 25x hybrid: 1.0s vs 39ms at N=385).
Callers opt in via {rerank: true}. Weights tunable via
{hybridWeight, ceWeight}; grid-search shows 0.3-0.5 hybrid all hit
the same plateau.

Cumulative since cosine baseline (3.10.17 → 3.10.20):

| Metric           | Cosine | 3.10.18 | 3.10.19 | 3.10.20 |
|------------------|-------:|--------:|--------:|--------:|
| Top-1 hit rate   |     0% |     50% |     80% |     90% |
| Top-3 hit rate   |     0% |     70% |     80% |    100% |
| MRR@3            |  0.000 |   0.583 |   0.800 |   0.933 |
| Top-1 diversity  |   100% |     80% |    100% |    100% |
| Latency (rerank) |   29ms |    41ms |    39ms |   984ms |

5 new tests cover the degradation contract (no network needed).

Co-Authored-By: RuFlo <ruv@ruv.net>
2026-05-30 14:20:34 -04:00
ruv 62df8a184d feat(intelligence): hybrid retrieval + outcome signal (ADR-078, 3.10.18)
Closes the relevance gap exposed by ADR-077: cosine-only neural_patterns
search was returning plausible-but-off-topic results because the bridge ONNX
bi-encoder gets distracted on small corpora by IDF-cheap shared tokens.

Three changes, one release:

1) Hybrid retrieval (src/memory/hybrid-retrieval.ts) — BM25 (k1=1.5,b=0.75)
   + cosine + MMR diversity (λ=0.5). Pure functions, no deps, 21 unit tests.
   Wired into neural_patterns search via {mode:'hybrid'|'cosine', alpha,
   mmrLambda, limit}. Cosine path preserved for A/B.

2) Pattern.content persistence — adds source text (cap 4096 chars) to the
   neural store so BM25 has tokens to score against. Backwards compatible:
   pre-3.10.18 patterns fall back to `name`.

3) Outcome signal in pretrain harvester — detects reverted (Revert "<X>"
   pattern) and hotfixed (≥50% file overlap with later fix commit) commits,
   surfacing the verdict mix in summary + recording original outcome in
   trajectory metadata.

Measured (N=385, 10 queries, this checkout):

| Metric                | Cosine | Hybrid | Δ       |
|-----------------------|-------:|-------:|--------:|
| Top-1 hit rate        |     0% |    50% |  +50pp  |
| Top-3 hit rate        |     0% |    70% |  +70pp  |
| MRR@3                 |  0.000 |  0.583 | +0.583  |
| Top-1 diversity       |   100% |    80% |  -20pp  |
| Avg query latency     | 28.7ms | 40.6ms | +11.9ms |

Cosine was returning 0% relevant top-3 results — finding "something" but
never the right thing. Hybrid lands relevant top-1 50% / top-3 70% with a
12 ms latency cost.

Co-Authored-By: RuFlo <ruv@ruv.net>
2026-05-30 13:20:56 -04:00
ruv 0aac1e13f1 feat(intelligence): pretrain self-learning from repo history (ADR-077, 3.10.17)
Ships a deterministic one-shot harvester so a fresh ruflo install can
bootstrap its self-learning system from the repo's own GitHub history
(git commits + gh issues) instead of starting empty.

Each item flows through the same code paths real-time learning uses —
distillAndSerialise (ADR-076) → recordTrajectory (ADR-074) → neural
store seed. Closes the ADR-075 consistency note "globalStats moved but
neural store stayed empty" by writing to both stores from the same script.

Measured (this checkout):
  • 80/80 trajectories trained from 50 commits + 30 issues
  • +95 trajectoriesRecorded, +85 patternsLearned, +80 neuralPatternCount
  • 3.36 ms avg per trajectory
  • 100% retrieval match rate across 10 sample queries
  • 7.67 ms avg query latency at N=95

Adds:
  - scripts/pretrain-from-github.mjs (harvester, env-configurable)
  - scripts/benchmark-pretrained-retrieval.mjs (after-pretrain validator)
  - __tests__/pretrain-from-github.test.ts (CI guard, embedded fixture,
    no git/gh shell-outs — auto-picked-up by v3-ci.yml line 610)
  - v3/docs/adr/ADR-077-pretrain-from-history.md (decision + measured
    proof + honest limits)
  - v3/docs/learning/self-learning-usage.md (copy-paste guide for all
    three learning paths plus this pretrain flow)

Co-Authored-By: RuFlo <ruv@ruv.net>
2026-05-30 12:55:58 -04:00