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

Method windowToCeiling

src/mcp/tools.ts:6271–6344  ·  view source on GitHub ↗
(
        parts: ReadonlyArray<SectionPart>,
        ceiling: number,
        focusLines: ReadonlyArray<number> = [],
      )

Source from the content-addressed store, hash-verified

6269 * the whole ceiling for source.
6270 */
6271 const windowToCeiling = (
6272 parts: ReadonlyArray<SectionPart>,
6273 ceiling: number,
6274 focusLines: ReadonlyArray<number> = [],
6275 ): SectionPart[] => {
6276 const inParts = (line: number) =>
6277 parts.some((p) => line >= p.range.start && line <= p.range.end);
6278 const focus = [...new Set(focusLines)]
6279 .filter((l) => typeof l === 'number' && l > 0 && inParts(l))
6280 .sort((a, b) => a - b);
6281 /** Source-ordered fill of whole parts, the overrunning one cut to a head window. */
6282 const fill = (room: number): { emit: ExploreLineRange[]; used: number } => {
6283 const emit: ExploreLineRange[] = [];
6284 let used = 0;
6285 for (const p of parts) {
6286 const join = emit.length > 0 ? GAP_MARKER.length : 0;
6287 if (used + join + p.text.length <= room) {
6288 emit.push(p.range);
6289 used += join + p.text.length;
6290 continue;
6291 }
6292 const first = emit.length === 0;
6293 const win = headWindowOf(
6294 p.range, Math.max(0, room - used - join), first ? MIN_WINDOW_LINES : 0);
6295 if (win && (first || win.end - win.start + 1 >= MIN_WINDOW_LINES)) {
6296 emit.push(win);
6297 used += join + renderSpan(win).length;
6298 }
6299 break;
6300 }
6301 return { emit, used };
6302 };
6303 let { emit, used } = fill(ceiling);
6304 const reached = () => (emit.length ? emit[emit.length - 1]!.end : 0);
6305 if (focus.some((l) => l > reached())) {
6306 // Hold room back for the focus windows so the head can't eat all of it.
6307 ({ emit, used } = fill(Math.floor(ceiling * 0.6)));
6308 }
6309 // What is left is SPLIT between the uncovered focus lines rather than
6310 // handed to them in order. Greedy-in-source-order reproduces the very bug
6311 // this guards: on a prose query resolving four focus lines, the two
6312 // earliest took the whole reserve and `flushQueuedMessages` at L1102 —
6313 // named in the question — was dropped again. A skipped or undersized
6314 // window returns its share to the pool for the ones after it.
6315 let covered = reached();
6316 let room = Math.max(0, ceiling - used);
6317 const pending = focus.filter((l) => l > covered);
6318 for (let i = 0; i < pending.length; i++) {
6319 const line = pending[i]!;
6320 if (line <= covered) continue; // an earlier window already reached it
6321 const share = Math.floor(room / (pending.length - i)) - GAP_MARKER.length;
6322 if (share <= 0) continue;
6323 const host = parts.find((p) => line >= p.range.start && line <= p.range.end)!;
6324 const lo = Math.max(host.range.start, line - SPINE_WINDOW, covered + 1);
6325 const hi = Math.min(host.range.end, line + SPINE_WINDOW);
6326 const win = centeredWindowOf(line, lo, hi, share);
6327 // Same sliver floor as the head window — a two-line peek at the call
6328 // site teaches the next call's dedup to shred the block around it.

Callers

nothing calls this directly

Calls 4

fillFunction · 0.85
pushMethod · 0.80
mapMethod · 0.80
reachedFunction · 0.50

Tested by

no test coverage detected