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

Function planFrontload

src/directory.ts:863–903  ·  view source on GitHub ↗
(cwd: string, prompt: string)

Source from the content-addressed store, hash-verified

861 * 3. **nothing indexed reachable** → do nothing (the agent's own tools apply).
862 */
863export function planFrontload(cwd: string, prompt: string): FrontloadPlan {
864 const none: FrontloadPlan = { exploreRoot: null, nudgeProjects: [], viaSubScan: false };
865
866 // 1. up-walk — nearest indexed ancestor (incl. cwd). Cheap; covers the common
867 // single-project case without a down-scan.
868 let dir = path.resolve(cwd);
869 for (let i = 0; i < 6; i++) {
870 if (isInitialized(dir)) return { exploreRoot: dir, nudgeProjects: [], viaSubScan: false };
871 const parent = path.dirname(dir);
872 if (parent === dir) break;
873 dir = parent;
874 }
875
876 // 2. down-scan — only from something that looks like a workspace root, so a
877 // non-project cwd (e.g. $HOME) is a cheap no-op, not a deep crawl.
878 const base = path.resolve(cwd);
879 if (!looksLikeProjectRoot(base)) return none;
880 const subs = findIndexedSubprojectRoots(base);
881 if (subs.length === 0) return none;
882 if (subs.length === 1) return { exploreRoot: subs[0]!, nudgeProjects: [], viaSubScan: true };
883
884 // Several indexed sub-projects — pick the one the prompt points at, if any.
885 const p = prompt.toLowerCase();
886 let best: { root: string; score: number; relLen: number } | null = null;
887 for (const s of subs) {
888 const rel = path.relative(base, s);
889 const relLc = rel.split(path.sep).join('/').toLowerCase();
890 const name = path.basename(s).toLowerCase();
891 let score = 0;
892 if (relLc && p.includes(relLc)) score = 10; // "packages/api"
893 else if (name.length >= 3 && new RegExp(`\\b${escapeRegExp(name)}\\b`).test(p)) score = 5; // "api"
894 if (score > 0 && (!best || score > best.score || (score === best.score && rel.length < best.relLen))) {
895 best = { root: s, score, relLen: rel.length };
896 }
897 }
898 if (best) {
899 return { exploreRoot: best.root, nudgeProjects: subs.filter((s) => s !== best!.root), viaSubScan: true };
900 }
901 // No clear match — nudge the full list rather than front-load a guess.
902 return { exploreRoot: null, nudgeProjects: subs, viaSubScan: true };
903}
904
905/**
906 * Contents of `.codegraph/.gitignore`. A single wildcard ignore keeps every

Callers 3

mainFunction · 0.90

Calls 6

isInitializedFunction · 0.85
looksLikeProjectRootFunction · 0.85
escapeRegExpFunction · 0.70
resolveMethod · 0.65
joinMethod · 0.45

Tested by

no test coverage detected