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

Method shrinkCluster

src/mcp/tools.ts:7185–7284  ·  view source on GitHub ↗
(
        c: ExploreCluster, cap: number, incidentalCap?: number,
      )

Source from the content-addressed store, hash-verified

7183 mergeKept(keep).flatMap((m) => sectionRangesOf(m)).reduce((sum, r, i) =>
7184 sum + renderedSpanCost(r.start, r.end) + (i > 0 ? GAP_MARKER.length : 0), 0);
7185 const shrinkCluster = (
7186 c: ExploreCluster, cap: number, incidentalCap?: number,
7187 ): SectionPart[] | null => {
7188 if (c.members.length < 2) return null;
7189 const guarded = incidentalCap !== undefined;
7190 // Protected members first when a hold-back applies (see `renderCluster`);
7191 // then an EXACT target, then the spine, as `rankedClusters` ranks spine
7192 // clusters first: the call path IS the answer. An unnamed bridge on it
7193 // (vscode's `_receiveRequest`, the Flow section's own step 2) otherwise
7194 // ranks with incidental helpers and is the member the cap drops. Exact
7195 // ahead of spine because the exact branch below keeps members in this
7196 // order until the cap: a long spine method first would price out the
7197 // body the agent singled out.
7198 //
7199 // Among members of one importance, a definition the query spelled by
7200 // name goes ahead of one its search only matched, and only then does the
7201 // shorter span win. The first member is the one kept however big it is,
7202 // so this decides which symbol that guarantee covers. Shorter-first gave
7203 // it to `path = build_report()` — a search hit on its signature, one
7204 // line — and `build_report`, the 200-line function the query named,
7205 // then no longer fit and the section was the file's last six lines
7206 // (#2486).
7207 const byImportance = [...c.members].sort((a, b) =>
7208 (guarded ? Number(isProtectedMember(b)) - Number(isProtectedMember(a)) : 0)
7209 || Number(b.importance >= EXACT_IMPORTANCE) - Number(a.importance >= EXACT_IMPORTANCE)
7210 || (b.spine ? 1 : 0) - (a.spine ? 1 : 0)
7211 || b.importance - a.importance
7212 || Number(!!b.named) - Number(!!a.named)
7213 || (a.end - a.start) - (b.end - b.start) || a.start - b.start);
7214 const keep: ExploreRange[] = [];
7215 if (c.maxImportance >= EXACT_IMPORTANCE) {
7216 // An EXACT target prices EVERY member in rendered chars, protected
7217 // ones included. The raw estimate below over-keeps on purpose and
7218 // leaves the ceiling trim to cut — but that trim fills in SOURCE
7219 // order, so any member above the exact body in the file (an entry
7220 // point, a named neighbour) was paid for first and the body the agent
7221 // asked for lost its tail (Django's `SQLCompiler.as_sql` stopped at
7222 // L944 of 1001 behind a 15-line neighbour). Priced exactly, the kept
7223 // set fits and the trim has nothing left to cut unless the exact body
7224 // ALONE overruns the ceiling — where it is windowed on its focus
7225 // lines. Incidental members also fit `incidentalCap`, as below.
7226 for (const r of byImportance) {
7227 const limit = guarded && !isProtectedMember(r) ? Math.min(cap, incidentalCap!) : cap;
7228 if (keep.length > 0 && renderedSizeOfKept([...keep, r]) > limit) continue;
7229 keep.push(r);
7230 }
7231 } else {
7232 // An oversize spine method costs its window, not its body — `buildSection`
7233 // windows it wherever it lands. Charging the full body kept it as the
7234 // most important member and then had no room left for anything beside it.
7235 const sizeOf = (r: ExploreRange) => (oversizeSpineSet.has(r) ? spineWindowOf(r) : [r])
7236 .reduce((n, w) => n + fileLines.slice(w.start - 1, w.end).join('\n').length, 0);
7237 let kept = 0;
7238 // Rendered size of what is kept so far, once the first incidental member
7239 // is weighed. Every protected member sorts ahead of it, so this is the
7240 // exact cost of the protected set; each incidental member then adds its
7241 // own padded span, an over-estimate that errs toward the cluster below.
7242 let rendered: number | undefined;

Callers

nothing calls this directly

Calls 4

NumberInterface · 0.85
sizeOfFunction · 0.85
pushMethod · 0.80
hasMethod · 0.80

Tested by

no test coverage detected