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

Method shrinkCluster

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

Source from the content-addressed store, hash-verified

7127 mergeKept(keep).flatMap((m) => sectionRangesOf(m)).reduce((sum, r, i) =>
7128 sum + renderedSpanCost(r.start, r.end) + (i > 0 ? GAP_MARKER.length : 0), 0);
7129 const shrinkCluster = (
7130 c: ExploreCluster, cap: number, incidentalCap?: number,
7131 ): SectionPart[] | null => {
7132 if (c.members.length < 2) return null;
7133 const guarded = incidentalCap !== undefined;
7134 // Protected members first when a hold-back applies (see `renderCluster`);
7135 // then an EXACT target, then the spine, as `rankedClusters` ranks spine
7136 // clusters first: the call path IS the answer. An unnamed bridge on it
7137 // (vscode's `_receiveRequest`, the Flow section's own step 2) otherwise
7138 // ranks with incidental helpers and is the member the cap drops. Exact
7139 // ahead of spine because the exact branch below keeps members in this
7140 // order until the cap: a long spine method first would price out the
7141 // body the agent singled out.
7142 //
7143 // Among members of one importance, a definition the query spelled by
7144 // name goes ahead of one its search only matched, and only then does the
7145 // shorter span win. The first member is the one kept however big it is,
7146 // so this decides which symbol that guarantee covers. Shorter-first gave
7147 // it to `path = build_report()` — a search hit on its signature, one
7148 // line — and `build_report`, the 200-line function the query named,
7149 // then no longer fit and the section was the file's last six lines
7150 // (#2486).
7151 const byImportance = [...c.members].sort((a, b) =>
7152 (guarded ? Number(isProtectedMember(b)) - Number(isProtectedMember(a)) : 0)
7153 || Number(b.importance >= EXACT_IMPORTANCE) - Number(a.importance >= EXACT_IMPORTANCE)
7154 || (b.spine ? 1 : 0) - (a.spine ? 1 : 0)
7155 || b.importance - a.importance
7156 || Number(!!b.named) - Number(!!a.named)
7157 || (a.end - a.start) - (b.end - b.start) || a.start - b.start);
7158 const keep: ExploreRange[] = [];
7159 if (c.maxImportance >= EXACT_IMPORTANCE) {
7160 // An EXACT target prices EVERY member in rendered chars, protected
7161 // ones included. The raw estimate below over-keeps on purpose and
7162 // leaves the ceiling trim to cut — but that trim fills in SOURCE
7163 // order, so any member above the exact body in the file (an entry
7164 // point, a named neighbour) was paid for first and the body the agent
7165 // asked for lost its tail (Django's `SQLCompiler.as_sql` stopped at
7166 // L944 of 1001 behind a 15-line neighbour). Priced exactly, the kept
7167 // set fits and the trim has nothing left to cut unless the exact body
7168 // ALONE overruns the ceiling — where it is windowed on its focus
7169 // lines. Incidental members also fit `incidentalCap`, as below.
7170 for (const r of byImportance) {
7171 const limit = guarded && !isProtectedMember(r) ? Math.min(cap, incidentalCap!) : cap;
7172 if (keep.length > 0 && renderedSizeOfKept([...keep, r]) > limit) continue;
7173 keep.push(r);
7174 }
7175 } else {
7176 // An oversize spine method costs its window, not its body — `buildSection`
7177 // windows it wherever it lands. Charging the full body kept it as the
7178 // most important member and then had no room left for anything beside it.
7179 const sizeOf = (r: ExploreRange) => (oversizeSpineSet.has(r) ? spineWindowOf(r) : [r])
7180 .reduce((n, w) => n + fileLines.slice(w.start - 1, w.end).join('\n').length, 0);
7181 let kept = 0;
7182 // Rendered size of what is kept so far, once the first incidental member
7183 // is weighed. Every protected member sorts ahead of it, so this is the
7184 // exact cost of the protected set; each incidental member then adds its
7185 // own padded span, an over-estimate that errs toward the cluster below.
7186 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