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

Method windowToCeiling

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

Source from the content-addressed store, hash-verified

6325 * the whole ceiling for source.
6326 */
6327 const windowToCeiling = (
6328 parts: ReadonlyArray<SectionPart>,
6329 ceiling: number,
6330 focusLines: ReadonlyArray<number> = [],
6331 ): SectionPart[] => {
6332 const inParts = (line: number) =>
6333 parts.some((p) => line >= p.range.start && line <= p.range.end);
6334 const focus = [...new Set(focusLines)]
6335 .filter((l) => typeof l === 'number' && l > 0 && inParts(l))
6336 .sort((a, b) => a - b);
6337 /** Source-ordered fill of whole parts, the overrunning one cut to a head window. */
6338 const fill = (room: number): { emit: ExploreLineRange[]; used: number } => {
6339 const emit: ExploreLineRange[] = [];
6340 let used = 0;
6341 for (const p of parts) {
6342 const join = emit.length > 0 ? GAP_MARKER.length : 0;
6343 if (used + join + p.text.length <= room) {
6344 emit.push(p.range);
6345 used += join + p.text.length;
6346 continue;
6347 }
6348 const first = emit.length === 0;
6349 const win = headWindowOf(
6350 p.range, Math.max(0, room - used - join), first ? MIN_WINDOW_LINES : 0);
6351 if (win && (first || win.end - win.start + 1 >= MIN_WINDOW_LINES)) {
6352 emit.push(win);
6353 used += join + renderSpan(win).length;
6354 }
6355 break;
6356 }
6357 return { emit, used };
6358 };
6359 let { emit, used } = fill(ceiling);
6360 const reached = () => (emit.length ? emit[emit.length - 1]!.end : 0);
6361 if (focus.some((l) => l > reached())) {
6362 // Hold room back for the focus windows so the head can't eat all of it.
6363 ({ emit, used } = fill(Math.floor(ceiling * 0.6)));
6364 }
6365 // What is left is SPLIT between the uncovered focus lines rather than
6366 // handed to them in order. Greedy-in-source-order reproduces the very bug
6367 // this guards: on a prose query resolving four focus lines, the two
6368 // earliest took the whole reserve and `flushQueuedMessages` at L1102 —
6369 // named in the question — was dropped again. A skipped or undersized
6370 // window returns its share to the pool for the ones after it.
6371 let covered = reached();
6372 let room = Math.max(0, ceiling - used);
6373 const pending = focus.filter((l) => l > covered);
6374 for (let i = 0; i < pending.length; i++) {
6375 const line = pending[i]!;
6376 if (line <= covered) continue; // an earlier window already reached it
6377 const share = Math.floor(room / (pending.length - i)) - GAP_MARKER.length;
6378 if (share <= 0) continue;
6379 const host = parts.find((p) => line >= p.range.start && line <= p.range.end)!;
6380 const lo = Math.max(host.range.start, line - SPINE_WINDOW, covered + 1);
6381 const hi = Math.min(host.range.end, line + SPINE_WINDOW);
6382 const win = centeredWindowOf(line, lo, hi, share);
6383 // Same sliver floor as the head window — a two-line peek at the call
6384 // 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