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

Method windowToCeiling

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

Source from the content-addressed store, hash-verified

6347 * the whole ceiling for source.
6348 */
6349 const windowToCeiling = (
6350 parts: ReadonlyArray<SectionPart>,
6351 ceiling: number,
6352 focusLines: ReadonlyArray<number> = [],
6353 ): SectionPart[] => {
6354 const inParts = (line: number) =>
6355 parts.some((p) => line >= p.range.start && line <= p.range.end);
6356 const focus = [...new Set(focusLines)]
6357 .filter((l) => typeof l === 'number' && l > 0 && inParts(l))
6358 .sort((a, b) => a - b);
6359 /** Source-ordered fill of whole parts, the overrunning one cut to a head window. */
6360 const fill = (room: number): { emit: ExploreLineRange[]; used: number } => {
6361 const emit: ExploreLineRange[] = [];
6362 let used = 0;
6363 for (const p of parts) {
6364 const join = emit.length > 0 ? GAP_MARKER.length : 0;
6365 if (used + join + p.text.length <= room) {
6366 emit.push(p.range);
6367 used += join + p.text.length;
6368 continue;
6369 }
6370 const first = emit.length === 0;
6371 const win = headWindowOf(
6372 p.range, Math.max(0, room - used - join), first ? MIN_WINDOW_LINES : 0);
6373 if (win && (first || win.end - win.start + 1 >= MIN_WINDOW_LINES)) {
6374 emit.push(win);
6375 used += join + renderSpan(win).length;
6376 }
6377 break;
6378 }
6379 return { emit, used };
6380 };
6381 let { emit, used } = fill(ceiling);
6382 const reached = () => (emit.length ? emit[emit.length - 1]!.end : 0);
6383 if (focus.some((l) => l > reached())) {
6384 // Hold room back for the focus windows so the head can't eat all of it.
6385 ({ emit, used } = fill(Math.floor(ceiling * 0.6)));
6386 }
6387 // What is left is SPLIT between the uncovered focus lines rather than
6388 // handed to them in order. Greedy-in-source-order reproduces the very bug
6389 // this guards: on a prose query resolving four focus lines, the two
6390 // earliest took the whole reserve and `flushQueuedMessages` at L1102 —
6391 // named in the question — was dropped again. A skipped or undersized
6392 // window returns its share to the pool for the ones after it.
6393 let covered = reached();
6394 let room = Math.max(0, ceiling - used);
6395 const pending = focus.filter((l) => l > covered);
6396 for (let i = 0; i < pending.length; i++) {
6397 const line = pending[i]!;
6398 if (line <= covered) continue; // an earlier window already reached it
6399 const share = Math.floor(room / (pending.length - i)) - GAP_MARKER.length;
6400 if (share <= 0) continue;
6401 const host = parts.find((p) => line >= p.range.start && line <= p.range.end)!;
6402 const lo = Math.max(host.range.start, line - SPINE_WINDOW, covered + 1);
6403 const hi = Math.min(host.range.end, line + SPINE_WINDOW);
6404 const win = centeredWindowOf(line, lo, hi, share);
6405 // Same sliver floor as the head window — a two-line peek at the call
6406 // site teaches the next call's dedup to shred the block around it.

Callers

nothing calls this directly

Calls 4

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

Tested by

no test coverage detected