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