MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / getSegmentMatches

Method getSegmentMatches

src/index.ts:2783–2859  ·  view source on GitHub ↗

* Graph-derived prompt matching for the front-load hook's MEDIUM tier: * which indexed symbols do these prose words name? "state machine des * commandes" → `OrderStateMachine`, in any human language whose technical * nouns are Latin script — no keyword list involved. * * Precision com

(words: string[], limit: number = 6)

Source from the content-addressed store, hash-verified

2781 * returned symbol is guaranteed to exist right now.
2782 */
2783 getSegmentMatches(words: string[], limit: number = 6): SegmentMatch[] {
2784 if (words.length === 0) return [];
2785 // Variant → original word (plural folding), for coverage accounting.
2786 const variantToWord = new Map<string, string>();
2787 for (const word of words) {
2788 for (const variant of segmentLookupVariants(word)) {
2789 if (!variantToWord.has(variant)) variantToWord.set(variant, word);
2790 }
2791 }
2792 const variants = [...variantToWord.keys()];
2793
2794 // Tier A: co-occurrence. The SQL folds variants back to their original
2795 // word (#1146), so minWords=2 means two distinct PROMPT WORDS — a name
2796 // matching both `service` and `services` can't tie with (or crowd past
2797 // the LIMIT) a genuine two-word match. The JS re-check below recomputes
2798 // the fold from live segments as the honesty layer.
2799 const variantPairs = [...variantToWord.entries()].map(([segment, word]) => ({ segment, word }));
2800 const candidates: Array<{ name: string; matchedWords: Set<string> }> = [];
2801 for (const hit of this.queries.getSegmentCoOccurrence(variantPairs, 2, 24)) {
2802 const matched = this.wordsMatchingName(hit.name, variantToWord);
2803 if (matched.size >= 2) candidates.push({ name: hit.name, matchedWords: matched });
2804 }
2805
2806 // Tier B: single rare word. Only when co-occurrence found nothing — a
2807 // co-occurring name is categorically stronger evidence — and under
2808 // stricter rules, because one word is thin: the word must be ≥5 chars
2809 // (measured FPs: "this", "typo"); the segment must appear in AT LEAST TWO
2810 // names (a concept the codebase is about clusters across names —
2811 // CheckoutService/CheckoutController — while a prose coincidence is a
2812 // singleton: measured FP "deploy to PRODUCTION" → the one name
2813 // matchesNonProductionDir); and the candidate name must have ≥2 segments
2814 // (a bare common verb matching a bare function name — "write" → `write` —
2815 // is prose coincidence, not the user naming a symbol).
2816 if (candidates.length === 0) {
2817 const singleWordVariants = variants.filter((v) => variantToWord.get(v)!.length >= 5);
2818 const counts = this.queries.getSegmentNameCounts(singleWordVariants);
2819 const rare = [...counts.entries()]
2820 .filter(([, n]) => n >= 2 && n <= CodeGraph.SEGMENT_RARITY_CEILING)
2821 .sort((a, b) => a[1] - b[1])
2822 .slice(0, 2);
2823 for (const [variant] of rare) {
2824 const word = variantToWord.get(variant)!;
2825 for (const name of this.queries.getNamesForSegment(variant, 12)) {
2826 if (splitIdentifierSegments(name).length < 2) continue;
2827 candidates.push({ name, matchedWords: new Set([word]) });
2828 }
2829 }
2830 }
2831
2832 // Verify against nodes (the honesty gate) and pick a representative
2833 // definition per name. A name whose only nodes are file/import kind has
2834 // no real definition to point at — surfacing the import statement instead
2835 // reads as a matched symbol but isn't one (#1144) — so it's skipped, the
2836 // same way an orphaned vocab row is. (Import names no longer enter the
2837 // vocab at write time, but rows written before that exclusion persist
2838 // until the next full index.)
2839 const out: SegmentMatch[] = [];
2840 const seen = new Set<string>();

Callers 3

findRelevantContextMethod · 0.95
mainFunction · 0.80

Calls 13

wordsMatchingNameMethod · 0.95
segmentLookupVariantsFunction · 0.90
splitIdentifierSegmentsFunction · 0.90
hasMethod · 0.80
mapMethod · 0.80
pushMethod · 0.80
getSegmentNameCountsMethod · 0.80
getNamesForSegmentMethod · 0.80
getMethod · 0.65
getNodesByNameMethod · 0.65
setMethod · 0.45

Tested by

no test coverage detected