geloforge 0.0.0-stage → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +72 -2
- package/THIRD_PARTY_NOTICES.md +33 -0
- package/VERSION +1 -0
- package/adapters/n8n/staging-audit-error-workflow.json +132 -0
- package/core/adopt-project.mjs +458 -0
- package/core/audit-learning.mjs +68 -0
- package/core/audit-provider.mjs +88 -0
- package/core/autonomous-audit.mjs +214 -0
- package/core/checkpoint.mjs +448 -0
- package/core/codebase-map.mjs +886 -0
- package/core/compatibility.json +16 -0
- package/core/compatibility.mjs +123 -0
- package/core/context-cache.mjs +113 -0
- package/core/context-compiler.mjs +1315 -0
- package/core/evidence-capture.mjs +347 -0
- package/core/git-utils.mjs +167 -0
- package/core/handoff.mjs +815 -0
- package/core/json-schema-lite.mjs +119 -0
- package/core/mcp-init.mjs +251 -0
- package/core/mcp-server.mjs +915 -0
- package/core/operation-approval.mjs +102 -0
- package/core/path-safety.mjs +508 -0
- package/core/project-locator.mjs +231 -0
- package/core/project-lock.mjs +438 -0
- package/core/project-status.mjs +635 -0
- package/core/project-validator.mjs +544 -0
- package/core/promotion.mjs +90 -0
- package/core/runtime-files.mjs +40 -0
- package/core/skills-history.mjs +572 -0
- package/core/skills-lifecycle.mjs +2225 -0
- package/core/skills-lint.mjs +365 -0
- package/core/skills.mjs +1449 -0
- package/core/staging-audit.mjs +454 -0
- package/core/staging-evidence.mjs +127 -0
- package/core/task-manager.mjs +1044 -0
- package/core/template-utils.mjs +190 -0
- package/core/text-utils.mjs +48 -0
- package/core/verify-task.mjs +48 -0
- package/core/worktrees.mjs +64 -0
- package/core/yaml-subset.mjs +572 -0
- package/package.json +50 -4
- package/policies/approval-boundaries.md +42 -0
- package/policies/memory-policy.md +53 -0
- package/policies/token-efficiency.md +30 -0
- package/schemas/README.md +43 -0
- package/schemas/adr.schema.json +64 -0
- package/schemas/agent-handoff.schema.json +139 -0
- package/schemas/approval-record.schema.json +110 -0
- package/schemas/conflict-record.schema.json +60 -0
- package/schemas/context-pack-manifest.schema.json +194 -0
- package/schemas/evidence-manifest.schema.json +83 -0
- package/schemas/incident-record.schema.json +53 -0
- package/schemas/project-manifest.schema.json +90 -0
- package/schemas/project-state.schema.json +64 -0
- package/schemas/recovery-checkpoint.schema.json +49 -0
- package/schemas/research-record.schema.json +76 -0
- package/schemas/task-contract.schema.json +95 -0
- package/scripts/bootstrap-project.mjs +357 -0
- package/scripts/geloforge.mjs +2512 -0
- package/templates/profiles/README.md +19 -0
- package/templates/profiles/ai-system/brain/profile.md +21 -0
- package/templates/profiles/ai-system/gitignore.append +5 -0
- package/templates/profiles/ai-system/profile.json +6 -0
- package/templates/profiles/automation/brain/profile.md +19 -0
- package/templates/profiles/automation/gitignore.append +5 -0
- package/templates/profiles/automation/profile.json +6 -0
- package/templates/profiles/desktop/brain/profile.md +21 -0
- package/templates/profiles/desktop/gitignore.append +7 -0
- package/templates/profiles/desktop/profile.json +6 -0
- package/templates/profiles/mobile/brain/profile.md +20 -0
- package/templates/profiles/mobile/gitignore.append +8 -0
- package/templates/profiles/mobile/profile.json +6 -0
- package/templates/profiles/web/brain/profile.md +21 -0
- package/templates/profiles/web/gitignore.append +6 -0
- package/templates/profiles/web/profile.json +6 -0
- package/templates/project-starter/AGENTS.md +27 -0
- package/templates/project-starter/GEMINI.md +16 -0
- package/templates/project-starter/README.md +26 -0
- package/templates/project-starter/brain/INDEX.md +40 -0
- package/templates/project-starter/brain/architecture/overview.md +30 -0
- package/templates/project-starter/brain/decisions/.gitkeep +0 -0
- package/templates/project-starter/brain/manifest.yaml +36 -0
- package/templates/project-starter/brain/requirements/constraints.md +29 -0
- package/templates/project-starter/brain/requirements/prd.md +37 -0
- package/templates/project-starter/brain/state/current.yaml +19 -0
- package/templates/project-starter/gitignore.template +25 -0
- package/templates/project-starter/src/.gitkeep +0 -0
- package/templates/project-starter/tasks/.gitkeep +0 -0
- package/templates/project-starter/tests/.gitkeep +0 -0
|
@@ -0,0 +1,886 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Selective Deterministic Codebase Mapper for GeloForge (Layer 4 navigational aid).
|
|
3
|
+
* Inspired by Aider's RepoMap concept: builds concise symbol outlines weighted by
|
|
4
|
+
* task/query relevance and local import relationships without paid APIs, embeddings,
|
|
5
|
+
* or loading entire large repositories into prompt context.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import fs from "node:fs";
|
|
9
|
+
import path from "node:path";
|
|
10
|
+
import { listGitVisibleFiles } from "./git-utils.mjs";
|
|
11
|
+
import { assertSafeWithinRoot, hasSecretContent, isForbiddenSecretPath } from "./path-safety.mjs";
|
|
12
|
+
|
|
13
|
+
const MAX_DISCOVERED_PATHS = 10000;
|
|
14
|
+
const DEFAULT_MAX_SCANNED_FILES = 500;
|
|
15
|
+
const DEFAULT_MAX_SELECTED_FILES = 25;
|
|
16
|
+
const DEFAULT_MAP_BUDGET = 1200;
|
|
17
|
+
const MAX_SOURCE_FILE_BYTES = 256 * 1024;
|
|
18
|
+
|
|
19
|
+
const EXCLUDED_DIRS = new Set([
|
|
20
|
+
".git",
|
|
21
|
+
".hg",
|
|
22
|
+
".svn",
|
|
23
|
+
"node_modules",
|
|
24
|
+
"dist",
|
|
25
|
+
"build",
|
|
26
|
+
"out",
|
|
27
|
+
"coverage",
|
|
28
|
+
".next",
|
|
29
|
+
".nuxt",
|
|
30
|
+
".svelte-kit",
|
|
31
|
+
".cache",
|
|
32
|
+
".turbo",
|
|
33
|
+
".parcel-cache",
|
|
34
|
+
"__pycache__",
|
|
35
|
+
".pytest_cache",
|
|
36
|
+
".mypy_cache",
|
|
37
|
+
".venv",
|
|
38
|
+
"venv",
|
|
39
|
+
"target",
|
|
40
|
+
"vendor",
|
|
41
|
+
"obj",
|
|
42
|
+
"secrets",
|
|
43
|
+
".aws",
|
|
44
|
+
".ssh",
|
|
45
|
+
]);
|
|
46
|
+
|
|
47
|
+
const SECOND_BRAIN_DIRS = new Set([
|
|
48
|
+
".geloforge",
|
|
49
|
+
"brain",
|
|
50
|
+
"tasks",
|
|
51
|
+
"evidence",
|
|
52
|
+
"proposals",
|
|
53
|
+
"handoffs",
|
|
54
|
+
"local-runtime",
|
|
55
|
+
"worktrees",
|
|
56
|
+
]);
|
|
57
|
+
|
|
58
|
+
const EXCLUDED_LOCKFILES = new Set([
|
|
59
|
+
"package-lock.json",
|
|
60
|
+
"pnpm-lock.yaml",
|
|
61
|
+
"yarn.lock",
|
|
62
|
+
"bun.lockb",
|
|
63
|
+
"cargo.lock",
|
|
64
|
+
"poetry.lock",
|
|
65
|
+
"pipfile.lock",
|
|
66
|
+
"composer.lock",
|
|
67
|
+
"gemfile.lock",
|
|
68
|
+
"go.sum",
|
|
69
|
+
]);
|
|
70
|
+
|
|
71
|
+
const BINARY_AND_ASSET_EXTS = new Set([
|
|
72
|
+
".png", ".jpg", ".jpeg", ".gif", ".webp", ".svg", ".ico", ".bmp", ".tiff",
|
|
73
|
+
".pdf", ".zip", ".tar", ".gz", ".tgz", ".bz2", ".xz", ".7z", ".rar",
|
|
74
|
+
".exe", ".dll", ".so", ".dylib", ".bin", ".wasm", ".o", ".a", ".lib",
|
|
75
|
+
".class", ".pyc", ".pyo", ".pyd",
|
|
76
|
+
".woff", ".woff2", ".ttf", ".otf", ".eot",
|
|
77
|
+
".mp3", ".wav", ".ogg", ".flac", ".mp4", ".mov", ".avi", ".webm",
|
|
78
|
+
".sqlite", ".sqlite3", ".db", ".map", ".crt", ".cer", ".der", ".csr",
|
|
79
|
+
".pdb", ".nupkg", ".jar", ".war", ".ear", ".suo", ".user",
|
|
80
|
+
]);
|
|
81
|
+
|
|
82
|
+
const MAP_STOP_WORDS = new Set([
|
|
83
|
+
"the", "and", "for", "with", "that", "this", "from", "into", "when", "what",
|
|
84
|
+
"where", "which", "will", "should", "must", "have", "has", "been", "they",
|
|
85
|
+
"their", "them", "then", "than", "only", "also", "more", "most", "some",
|
|
86
|
+
"such", "each", "other", "about", "after", "before", "using", "use", "used",
|
|
87
|
+
"task", "project", "file", "files", "code", "implement", "update", "fix",
|
|
88
|
+
]);
|
|
89
|
+
|
|
90
|
+
function estimateTokens(text) {
|
|
91
|
+
if (!text) return 0;
|
|
92
|
+
return Math.ceil(Buffer.byteLength(text, "utf8") / 4);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function sanitizeInline(text) {
|
|
96
|
+
return String(text || "")
|
|
97
|
+
.replace(/<<<END_GELOFORGE_DOC>>>/g, "<<<ESCAPED_END_GELOFORGE_DOC>>>")
|
|
98
|
+
.replace(/<<<GELOFORGE_DOC/g, "<<<ESCAPED_GELOFORGE_DOC")
|
|
99
|
+
.replace(/[\r\n]+/g, " ")
|
|
100
|
+
.trim();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function tokenizeKeywords(text) {
|
|
104
|
+
if (!text || typeof text !== "string") return new Set();
|
|
105
|
+
const raw = text
|
|
106
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
|
|
107
|
+
.toLowerCase()
|
|
108
|
+
.split(/[^a-z0-9]+/)
|
|
109
|
+
.filter((t) => t.length >= 2 && !MAP_STOP_WORDS.has(t));
|
|
110
|
+
return new Set(raw);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function isExcludedPath(relPath, { excludeSecondBrainDirs = true } = {}) {
|
|
114
|
+
const norm = relPath.replace(/\\/g, "/").replace(/^\.\//, "");
|
|
115
|
+
if (!norm) return "empty-path";
|
|
116
|
+
if (isForbiddenSecretPath(norm)) return "forbidden-secret-path";
|
|
117
|
+
|
|
118
|
+
const segments = norm.split("/").filter(Boolean);
|
|
119
|
+
const baseLower = segments[segments.length - 1].toLowerCase();
|
|
120
|
+
if (
|
|
121
|
+
baseLower === ".ds_store" ||
|
|
122
|
+
baseLower === "secrets.json" ||
|
|
123
|
+
baseLower === "secrets.yaml" ||
|
|
124
|
+
baseLower === "secrets.yml" ||
|
|
125
|
+
baseLower.startsWith("secrets.")
|
|
126
|
+
) {
|
|
127
|
+
return baseLower === ".ds_store" ? "binary-or-asset" : "forbidden-secret-path";
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
for (let i = 0; i < segments.length - 1; i += 1) {
|
|
131
|
+
const dirLower = segments[i].toLowerCase();
|
|
132
|
+
if (EXCLUDED_DIRS.has(dirLower)) return "excluded-dir";
|
|
133
|
+
if (dirLower === "bin" && i + 1 < segments.length - 1) {
|
|
134
|
+
const nextLower = segments[i + 1].toLowerCase();
|
|
135
|
+
if (nextLower === "debug" || nextLower === "release") {
|
|
136
|
+
return "excluded-dir";
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (excludeSecondBrainDirs && i === 0 && SECOND_BRAIN_DIRS.has(dirLower)) {
|
|
140
|
+
return "second-brain-dir";
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (EXCLUDED_LOCKFILES.has(baseLower)) return "lockfile";
|
|
145
|
+
if (baseLower.endsWith(".min.js") || baseLower.endsWith(".min.css")) return "minified-bundle";
|
|
146
|
+
const ext = path.extname(baseLower);
|
|
147
|
+
if (BINARY_AND_ASSET_EXTS.has(ext)) return "binary-or-asset";
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function compileGitignoreMatcher(gitignoreText, baseRelDir = "") {
|
|
152
|
+
const rules = [];
|
|
153
|
+
const lines = gitignoreText.split(/\r?\n/);
|
|
154
|
+
for (const rawLine of lines) {
|
|
155
|
+
const trimmed = rawLine.trim();
|
|
156
|
+
if (!trimmed || trimmed.startsWith("#")) continue;
|
|
157
|
+
|
|
158
|
+
let negate = false;
|
|
159
|
+
let pat = trimmed;
|
|
160
|
+
if (pat.startsWith("!")) {
|
|
161
|
+
negate = true;
|
|
162
|
+
pat = pat.slice(1);
|
|
163
|
+
}
|
|
164
|
+
const dirOnly = pat.endsWith("/");
|
|
165
|
+
if (dirOnly) pat = pat.slice(0, -1);
|
|
166
|
+
const anchored = pat.startsWith("/");
|
|
167
|
+
if (anchored) pat = pat.slice(1);
|
|
168
|
+
if (!pat) continue;
|
|
169
|
+
|
|
170
|
+
const hasSlash = pat.includes("/");
|
|
171
|
+
const escaped = pat
|
|
172
|
+
.replace(/\*\*\//g, "\0GLOBSTAR_DIR\0")
|
|
173
|
+
.replace(/\*\*/g, "\0GLOBSTAR\0")
|
|
174
|
+
.replace(/[.+^${}()|[\]\\]/g, "\\$&")
|
|
175
|
+
.replace(/\*/g, "[^/]*")
|
|
176
|
+
.replace(/\?/g, "[^/]")
|
|
177
|
+
.replace(/\0GLOBSTAR_DIR\0/g, "(?:.*/)?")
|
|
178
|
+
.replace(/\0GLOBSTAR\0/g, ".*");
|
|
179
|
+
const regexStr = anchored || hasSlash
|
|
180
|
+
? `^${escaped}(?:/.*)?$`
|
|
181
|
+
: `(?:^|/)${escaped}(?:/.*)?$`;
|
|
182
|
+
rules.push({ negate, dirOnly, regex: new RegExp(regexStr, "i"), baseRelDir });
|
|
183
|
+
}
|
|
184
|
+
return rules;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function isIgnoredByRules(relPath, isDir, rules) {
|
|
188
|
+
const norm = relPath.replace(/\\/g, "/");
|
|
189
|
+
let ignored = false;
|
|
190
|
+
for (const rule of rules) {
|
|
191
|
+
let target = norm;
|
|
192
|
+
if (rule.baseRelDir) {
|
|
193
|
+
const prefix = `${rule.baseRelDir}/`;
|
|
194
|
+
if (!norm.startsWith(prefix)) continue;
|
|
195
|
+
target = norm.slice(prefix.length);
|
|
196
|
+
}
|
|
197
|
+
if (rule.dirOnly && !isDir) {
|
|
198
|
+
if (!target.includes("/")) {
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
const parentDir = path.posix.dirname(target);
|
|
202
|
+
if (rule.regex.test(parentDir)) {
|
|
203
|
+
ignored = !rule.negate;
|
|
204
|
+
}
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
if (rule.regex.test(target)) {
|
|
208
|
+
ignored = !rule.negate;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return ignored;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function discoverFilesFallback(projectDir, warnings, excludeSecondBrainDirs) {
|
|
215
|
+
const discovered = [];
|
|
216
|
+
let hitMaxCap = false;
|
|
217
|
+
|
|
218
|
+
const walk = (absDir, inheritedRules) => {
|
|
219
|
+
if (discovered.length >= MAX_DISCOVERED_PATHS) {
|
|
220
|
+
hitMaxCap = true;
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
let rules = inheritedRules;
|
|
224
|
+
const giPath = path.join(absDir, ".gitignore");
|
|
225
|
+
if (fs.existsSync(giPath)) {
|
|
226
|
+
try {
|
|
227
|
+
const giStat = fs.lstatSync(giPath);
|
|
228
|
+
if (giStat.isFile() && !giStat.isSymbolicLink() && giStat.size <= 64 * 1024) {
|
|
229
|
+
const baseRel = path.relative(projectDir, absDir).replace(/\\/g, "/");
|
|
230
|
+
rules = [
|
|
231
|
+
...inheritedRules,
|
|
232
|
+
...compileGitignoreMatcher(fs.readFileSync(giPath, "utf8"), baseRel),
|
|
233
|
+
];
|
|
234
|
+
}
|
|
235
|
+
} catch {
|
|
236
|
+
// Ignore unreadable .gitignore
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const entries = fs.readdirSync(absDir, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name));
|
|
241
|
+
for (const entry of entries) {
|
|
242
|
+
if (discovered.length >= MAX_DISCOVERED_PATHS) {
|
|
243
|
+
hitMaxCap = true;
|
|
244
|
+
break;
|
|
245
|
+
}
|
|
246
|
+
const full = path.join(absDir, entry.name);
|
|
247
|
+
const rel = path.relative(projectDir, full).replace(/\\/g, "/");
|
|
248
|
+
let stat;
|
|
249
|
+
try {
|
|
250
|
+
stat = fs.lstatSync(full);
|
|
251
|
+
} catch {
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
if (stat.isSymbolicLink()) {
|
|
255
|
+
warnings.push(`Skipped symbolic link during codebase scan: ${rel}`);
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
if (stat.isDirectory()) {
|
|
259
|
+
const dirLower = entry.name.toLowerCase();
|
|
260
|
+
if (EXCLUDED_DIRS.has(dirLower)) continue;
|
|
261
|
+
if (excludeSecondBrainDirs && !rel.includes("/") && SECOND_BRAIN_DIRS.has(dirLower)) continue;
|
|
262
|
+
if (isIgnoredByRules(rel, true, rules)) continue;
|
|
263
|
+
walk(full, rules);
|
|
264
|
+
} else if (stat.isFile()) {
|
|
265
|
+
if (entry.name === ".gitignore") continue;
|
|
266
|
+
if (isIgnoredByRules(rel, false, rules)) continue;
|
|
267
|
+
discovered.push(rel);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
walk(projectDir, []);
|
|
273
|
+
if (hitMaxCap) {
|
|
274
|
+
warnings.push(`Directory discovery stopped after ${MAX_DISCOVERED_PATHS} files (bounded scan safeguard).`);
|
|
275
|
+
}
|
|
276
|
+
return { mode: "filesystem-walk", paths: discovered };
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function detectLanguage(relPath) {
|
|
280
|
+
const ext = path.extname(relPath).toLowerCase();
|
|
281
|
+
switch (ext) {
|
|
282
|
+
case ".js":
|
|
283
|
+
case ".mjs":
|
|
284
|
+
case ".cjs":
|
|
285
|
+
case ".jsx":
|
|
286
|
+
return "javascript";
|
|
287
|
+
case ".ts":
|
|
288
|
+
case ".mts":
|
|
289
|
+
case ".cts":
|
|
290
|
+
case ".tsx":
|
|
291
|
+
return "typescript";
|
|
292
|
+
case ".py":
|
|
293
|
+
case ".pyw":
|
|
294
|
+
return "python";
|
|
295
|
+
case ".go":
|
|
296
|
+
return "go";
|
|
297
|
+
case ".rs":
|
|
298
|
+
return "rust";
|
|
299
|
+
case ".java":
|
|
300
|
+
return "java";
|
|
301
|
+
case ".cs":
|
|
302
|
+
return "csharp";
|
|
303
|
+
case ".kt":
|
|
304
|
+
case ".kts":
|
|
305
|
+
return "kotlin";
|
|
306
|
+
case ".c":
|
|
307
|
+
case ".h":
|
|
308
|
+
case ".cc":
|
|
309
|
+
case ".cpp":
|
|
310
|
+
case ".hpp":
|
|
311
|
+
case ".hh":
|
|
312
|
+
return "c_cpp";
|
|
313
|
+
case ".sh":
|
|
314
|
+
case ".bash":
|
|
315
|
+
case ".zsh":
|
|
316
|
+
case ".ps1":
|
|
317
|
+
return "shell";
|
|
318
|
+
case ".sql":
|
|
319
|
+
return "sql";
|
|
320
|
+
case ".md":
|
|
321
|
+
case ".mdx":
|
|
322
|
+
return "markdown";
|
|
323
|
+
case ".json":
|
|
324
|
+
return "json";
|
|
325
|
+
case ".yaml":
|
|
326
|
+
case ".yml":
|
|
327
|
+
return "yaml";
|
|
328
|
+
case ".toml":
|
|
329
|
+
return "toml";
|
|
330
|
+
default:
|
|
331
|
+
return ext ? `unsupported(${ext})` : "unsupported";
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
export function extractFileOutline(relPath, content) {
|
|
336
|
+
const language = detectLanguage(relPath);
|
|
337
|
+
const lines = content.split(/\r?\n/);
|
|
338
|
+
const symbols = [];
|
|
339
|
+
const imports = [];
|
|
340
|
+
|
|
341
|
+
const pushSym = (lineNum, kind, name, sig) => {
|
|
342
|
+
const cleanSig = sanitizeInline(sig).slice(0, 110);
|
|
343
|
+
symbols.push({
|
|
344
|
+
line: lineNum,
|
|
345
|
+
kind,
|
|
346
|
+
name: sanitizeInline(name),
|
|
347
|
+
signature: cleanSig,
|
|
348
|
+
});
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
if (language === "javascript" || language === "typescript") {
|
|
352
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
353
|
+
const line = lines[i];
|
|
354
|
+
const trimmed = line.trim();
|
|
355
|
+
const ln = i + 1;
|
|
356
|
+
const impMatch = trimmed.match(/^(?:import\s+.*?\s+from\s+|import\s+|.*?\brequire\()\s*['"]([^'"]+)['"]/);
|
|
357
|
+
if (impMatch) imports.push(impMatch[1]);
|
|
358
|
+
|
|
359
|
+
if (trimmed.startsWith("//") || trimmed.startsWith("*")) continue;
|
|
360
|
+
let m;
|
|
361
|
+
if ((m = line.match(/^\s*(?:export\s+(?:default\s+)?)?(?:async\s+)?function\s+([A-Za-z_$][\w$]*)\s*\(([^)]*)\)/))) {
|
|
362
|
+
pushSym(ln, "function", m[1], `function ${m[1]}(${m[2].trim()})`);
|
|
363
|
+
} else if ((m = line.match(/^\s*(?:export\s+(?:default\s+)?)?(?:abstract\s+)?class\s+([A-Za-z_$][\w$]*)/))) {
|
|
364
|
+
pushSym(ln, "class", m[1], trimmed.replace(/\{.*$/, "").trim());
|
|
365
|
+
} else if ((m = line.match(/^\s*(?:export\s+)?(interface|type|enum)\s+([A-Za-z_$][\w$]*)/))) {
|
|
366
|
+
pushSym(ln, m[1], m[2], trimmed.replace(/\{.*$/, "").trim());
|
|
367
|
+
} else if ((m = line.match(/^\s*export\s+(?:const|let)\s+([A-Za-z_$][\w$]*)\s*=\s*(?:async\s*)?\(/))) {
|
|
368
|
+
pushSym(ln, "function", m[1], trimmed.replace(/=>.*$/, "=> ...").trim());
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
return { language, outlineMode: "ast-regex", symbols, imports, lineCount: lines.length };
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
if (language === "python") {
|
|
375
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
376
|
+
const line = lines[i];
|
|
377
|
+
const trimmed = line.trim();
|
|
378
|
+
const ln = i + 1;
|
|
379
|
+
let m;
|
|
380
|
+
if ((m = trimmed.match(/^from\s+(\S+)\s+import\s+/))) imports.push(m[1]);
|
|
381
|
+
else if ((m = trimmed.match(/^import\s+([A-Za-z_][\w.]*)/))) imports.push(m[1]);
|
|
382
|
+
|
|
383
|
+
if ((m = line.match(/^(\s*)(?:async\s+)?def\s+([A-Za-z_]\w*)\s*\(([^)]*)\)/))) {
|
|
384
|
+
const kind = m[1].length > 0 ? "method" : "function";
|
|
385
|
+
pushSym(ln, kind, m[2], `def ${m[2]}(${m[3].trim()})`);
|
|
386
|
+
} else if ((m = line.match(/^\s*class\s+([A-Za-z_]\w*(?:\([^)]*\))?)\s*:/))) {
|
|
387
|
+
pushSym(ln, "class", m[1].split("(")[0], `class ${m[1]}`);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
return { language, outlineMode: "ast-regex", symbols, imports, lineCount: lines.length };
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
if (language === "go") {
|
|
394
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
395
|
+
const line = lines[i];
|
|
396
|
+
const trimmed = line.trim();
|
|
397
|
+
const ln = i + 1;
|
|
398
|
+
let m;
|
|
399
|
+
if ((m = trimmed.match(/^import\s+(?:[A-Za-z_]\w*\s+)?"([^"]+)"/))) imports.push(m[1]);
|
|
400
|
+
if ((m = line.match(/^func\s+(?:\([^)]+\)\s+)?([A-Za-z_]\w*)\s*\(([^)]*)\)/))) {
|
|
401
|
+
pushSym(ln, "function", m[1], trimmed.replace(/\{.*$/, "").trim());
|
|
402
|
+
} else if ((m = line.match(/^type\s+([A-Za-z_]\w*)\s+(struct|interface)\b/))) {
|
|
403
|
+
pushSym(ln, m[2], m[1], `type ${m[1]} ${m[2]}`);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
return { language, outlineMode: "ast-regex", symbols, imports, lineCount: lines.length };
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
if (language === "rust") {
|
|
410
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
411
|
+
const line = lines[i];
|
|
412
|
+
const trimmed = line.trim();
|
|
413
|
+
const ln = i + 1;
|
|
414
|
+
let m;
|
|
415
|
+
if ((m = trimmed.match(/^use\s+([^;]+);/))) imports.push(m[1]);
|
|
416
|
+
if ((m = line.match(/^\s*(?:pub(?:\([^)]+\))?\s+)?(?:async\s+)?fn\s+([A-Za-z_]\w*)/))) {
|
|
417
|
+
pushSym(ln, "fn", m[1], trimmed.replace(/\{.*$/, "").trim());
|
|
418
|
+
} else if ((m = line.match(/^\s*(?:pub(?:\([^)]+\))?\s+)?(struct|enum|trait|mod)\s+([A-Za-z_]\w*)/))) {
|
|
419
|
+
pushSym(ln, m[1], m[2], trimmed.replace(/[{;].*$/, "").trim());
|
|
420
|
+
} else if ((m = line.match(/^\s*impl(?:<[^>]+>)?\s+([^{]+)/))) {
|
|
421
|
+
pushSym(ln, "impl", m[1].trim(), `impl ${m[1].trim()}`);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
return { language, outlineMode: "ast-regex", symbols, imports, lineCount: lines.length };
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
if (language === "java" || language === "csharp" || language === "kotlin") {
|
|
428
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
429
|
+
const line = lines[i];
|
|
430
|
+
const trimmed = line.trim();
|
|
431
|
+
const ln = i + 1;
|
|
432
|
+
let m;
|
|
433
|
+
if ((m = trimmed.match(/^(?:import|using)\s+([^;]+)/))) imports.push(m[1]);
|
|
434
|
+
if (
|
|
435
|
+
(m = line.match(
|
|
436
|
+
/^\s*(?:(?:public|private|protected|internal|static|final|abstract|sealed|data|open)\s+)*(class|interface|enum|record|struct)\s+([A-Za-z_]\w*)/
|
|
437
|
+
))
|
|
438
|
+
) {
|
|
439
|
+
pushSym(ln, m[1], m[2], trimmed.replace(/\{.*$/, "").trim());
|
|
440
|
+
} else if (
|
|
441
|
+
language === "kotlin" &&
|
|
442
|
+
(m = line.match(/^\s*(?:(?:public|private|internal|suspend)\s+)*fun\s+([A-Za-z_]\w*\s*\([^)]*\))/))
|
|
443
|
+
) {
|
|
444
|
+
pushSym(ln, "function", m[1].split("(")[0], `fun ${m[1]}`);
|
|
445
|
+
} else if (
|
|
446
|
+
(m = line.match(
|
|
447
|
+
/^\s*(?:public|private|protected|internal)\s+(?:static\s+|async\s+|virtual\s+|override\s+)*[\w<>[\], ?]+\s+([A-Za-z_]\w*)\s*\(([^)]*)\)/
|
|
448
|
+
))
|
|
449
|
+
) {
|
|
450
|
+
pushSym(ln, "method", m[1], trimmed.replace(/\{.*$/, "").trim());
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
return { language, outlineMode: "ast-regex", symbols, imports, lineCount: lines.length };
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
if (language === "c_cpp") {
|
|
457
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
458
|
+
const line = lines[i];
|
|
459
|
+
const trimmed = line.trim();
|
|
460
|
+
const ln = i + 1;
|
|
461
|
+
let m;
|
|
462
|
+
if ((m = trimmed.match(/^#include\s+"([^"]+)"/))) imports.push(m[1]);
|
|
463
|
+
if ((m = line.match(/^\s*(?:template\s*<[^>]+>\s*)?(class|struct|enum|namespace)\s+([A-Za-z_]\w*)/))) {
|
|
464
|
+
pushSym(ln, m[1], m[2], `${m[1]} ${m[2]}`);
|
|
465
|
+
} else if (
|
|
466
|
+
!trimmed.startsWith("if") &&
|
|
467
|
+
!trimmed.startsWith("for") &&
|
|
468
|
+
!trimmed.startsWith("while") &&
|
|
469
|
+
(m = line.match(/^[A-Za-z_][\w:*&<> ]+\s+([A-Za-z_]\w*)\s*\(([^;)]*)\)\s*(?:\{|$)/))
|
|
470
|
+
) {
|
|
471
|
+
pushSym(ln, "function", m[1], trimmed.replace(/\{.*$/, "").trim());
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
return { language, outlineMode: "ast-regex", symbols, imports, lineCount: lines.length };
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
if (language === "shell" || language === "sql" || language === "markdown" || language === "yaml" || language === "toml" || language === "json") {
|
|
478
|
+
if (language === "json") {
|
|
479
|
+
try {
|
|
480
|
+
const parsed = JSON.parse(content);
|
|
481
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
482
|
+
for (const key of Object.keys(parsed).slice(0, 12)) {
|
|
483
|
+
pushSym(1, "key", key, key);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
} catch {
|
|
487
|
+
// Fallback silently for invalid/JSONC files
|
|
488
|
+
}
|
|
489
|
+
return { language, outlineMode: "structured", symbols, imports, lineCount: lines.length };
|
|
490
|
+
}
|
|
491
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
492
|
+
const line = lines[i];
|
|
493
|
+
const ln = i + 1;
|
|
494
|
+
let m;
|
|
495
|
+
if (language === "markdown" && (m = line.match(/^(#{1,3})\s+(.+)$/))) {
|
|
496
|
+
pushSym(ln, `h${m[1].length}`, m[2], `${m[1]} ${m[2]}`);
|
|
497
|
+
} else if ((language === "yaml" || language === "toml") && (m = line.match(/^([A-Za-z_][\w.-]*)\s*:/) || line.match(/^\[([A-Za-z_][\w.-]*)\]/))) {
|
|
498
|
+
pushSym(ln, "section", m[1], m[1]);
|
|
499
|
+
} else if (language === "shell" && (m = line.match(/^\s*(?:function\s+)?([A-Za-z_][\w-]*)\s*\(\)\s*\{/))) {
|
|
500
|
+
pushSym(ln, "function", m[1], `${m[1]}()`);
|
|
501
|
+
} else if (language === "sql" && (m = line.match(/^\s*create\s+(?:or\s+replace\s+)?(table|view|function|index)\s+(?:if\s+not\s+exists\s+)?([A-Za-z_][\w."]*)/i))) {
|
|
502
|
+
pushSym(ln, m[1].toLowerCase(), m[2], line.trim());
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
return { language, outlineMode: "structured", symbols, imports, lineCount: lines.length };
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
// Graceful fallback for unsupported or custom text extensions
|
|
509
|
+
for (let i = 0; i < Math.min(lines.length, 60); i += 1) {
|
|
510
|
+
const trimmed = lines[i].trim();
|
|
511
|
+
if (!trimmed) continue;
|
|
512
|
+
const gen = trimmed.match(/^(?:pub\s+|export\s+)?(fn|def|func|function|class|struct|module|contract)\s+([A-Za-z_][\w]*)/);
|
|
513
|
+
if (gen) {
|
|
514
|
+
pushSym(i + 1, gen[1], gen[2], trimmed.slice(0, 90));
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
if (symbols.length === 0) {
|
|
518
|
+
for (let i = 0; i < Math.min(lines.length, 15) && symbols.length < 2; i += 1) {
|
|
519
|
+
const trimmed = sanitizeInline(lines[i]);
|
|
520
|
+
if (trimmed.length >= 4) {
|
|
521
|
+
pushSym(i + 1, "line", trimmed.slice(0, 40), trimmed.slice(0, 90));
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
return { language, outlineMode: "fallback", symbols, imports, lineCount: lines.length };
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
function resolveLocalImport(fromRelPath, specifier, candidateSet) {
|
|
529
|
+
if (!specifier) {
|
|
530
|
+
return null;
|
|
531
|
+
}
|
|
532
|
+
let normalizedSpec = specifier;
|
|
533
|
+
if (/^\.+[A-Za-z_]/.test(specifier)) {
|
|
534
|
+
const dotMatch = specifier.match(/^(\.+)(.*)$/);
|
|
535
|
+
if (dotMatch) {
|
|
536
|
+
const dots = dotMatch[1];
|
|
537
|
+
const modPath = dotMatch[2].replace(/\./g, "/");
|
|
538
|
+
const prefix = dots.length === 1 ? "./" : "../".repeat(dots.length - 1);
|
|
539
|
+
normalizedSpec = `${prefix}${modPath}`;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
if (!normalizedSpec.startsWith(".") && !normalizedSpec.startsWith("/")) {
|
|
543
|
+
return null;
|
|
544
|
+
}
|
|
545
|
+
const fromDir = path.posix.dirname(fromRelPath.replace(/\\/g, "/"));
|
|
546
|
+
const base = normalizedSpec.startsWith("/")
|
|
547
|
+
? normalizedSpec.slice(1)
|
|
548
|
+
: path.posix.normalize(path.posix.join(fromDir, normalizedSpec));
|
|
549
|
+
|
|
550
|
+
const trials = [
|
|
551
|
+
base,
|
|
552
|
+
`${base}.mjs`,
|
|
553
|
+
`${base}.js`,
|
|
554
|
+
`${base}.ts`,
|
|
555
|
+
`${base}.tsx`,
|
|
556
|
+
`${base}.jsx`,
|
|
557
|
+
`${base}.py`,
|
|
558
|
+
`${base}/index.mjs`,
|
|
559
|
+
`${base}/index.js`,
|
|
560
|
+
`${base}/index.ts`,
|
|
561
|
+
];
|
|
562
|
+
for (const t of trials) {
|
|
563
|
+
if (candidateSet.has(t)) return t;
|
|
564
|
+
}
|
|
565
|
+
return null;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
export function buildCodebaseMap(options = {}) {
|
|
569
|
+
if (typeof options.projectDir !== "string" || options.projectDir.trim().length === 0) {
|
|
570
|
+
throw new Error("buildCodebaseMap requires options.projectDir.");
|
|
571
|
+
}
|
|
572
|
+
const projectDir = path.resolve(options.projectDir);
|
|
573
|
+
const warnings = Array.isArray(options.warnings) ? options.warnings : [];
|
|
574
|
+
const mapBudget = Number.isInteger(options.mapBudget) && options.mapBudget >= 100 ? options.mapBudget : DEFAULT_MAP_BUDGET;
|
|
575
|
+
const maxScannedFiles =
|
|
576
|
+
Number.isInteger(options.maxScannedFiles) && options.maxScannedFiles > 0
|
|
577
|
+
? options.maxScannedFiles
|
|
578
|
+
: DEFAULT_MAX_SCANNED_FILES;
|
|
579
|
+
const maxSelectedFiles =
|
|
580
|
+
Number.isInteger(options.maxFiles) && options.maxFiles > 0 ? options.maxFiles : DEFAULT_MAX_SELECTED_FILES;
|
|
581
|
+
const maxSymbolsPerFile =
|
|
582
|
+
Number.isInteger(options.maxSymbolsPerFile) && options.maxSymbolsPerFile > 0 ? options.maxSymbolsPerFile : 12;
|
|
583
|
+
const excludeSecondBrainDirs = options.excludeSecondBrainDirs !== false;
|
|
584
|
+
|
|
585
|
+
const seedPaths = new Set(Array.from(options.seedPaths || [], (p) => String(p).replace(/\\/g, "/")));
|
|
586
|
+
const skipPaths = new Set(Array.from(options.alreadyIncludedPaths || [], (p) => String(p).replace(/\\/g, "/")));
|
|
587
|
+
const queryKeywords = options.queryKeywords instanceof Set ? options.queryKeywords : tokenizeKeywords(options.queryText || "");
|
|
588
|
+
const queryMentionsTests = queryKeywords.has("test") || queryKeywords.has("tests") || queryKeywords.has("spec");
|
|
589
|
+
|
|
590
|
+
// 1. Discover files (Git .gitignore-aware when project root is Git root, deterministic walk otherwise)
|
|
591
|
+
const gitDiscovery = listGitVisibleFiles(projectDir, warnings);
|
|
592
|
+
const rawDiscovery = gitDiscovery || discoverFilesFallback(projectDir, warnings, excludeSecondBrainDirs);
|
|
593
|
+
const scanMode = rawDiscovery.mode;
|
|
594
|
+
|
|
595
|
+
// 2. Filter paths cleanly before reading any bytes
|
|
596
|
+
const blockedFiles = [];
|
|
597
|
+
const eligiblePaths = [];
|
|
598
|
+
for (const rawRel of rawDiscovery.paths) {
|
|
599
|
+
const rel = rawRel.replace(/\\/g, "/");
|
|
600
|
+
const excludedReason = isExcludedPath(rel, { excludeSecondBrainDirs });
|
|
601
|
+
if (excludedReason) {
|
|
602
|
+
if (excludedReason === "forbidden-secret-path") {
|
|
603
|
+
blockedFiles.push({ path: rel, reason: "Excluded sensitive credential/key path" });
|
|
604
|
+
}
|
|
605
|
+
continue;
|
|
606
|
+
}
|
|
607
|
+
eligiblePaths.push(rel);
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
// Deduplicate + sort cleanly
|
|
611
|
+
const uniqueEligible = Array.from(new Set(eligiblePaths)).sort((a, b) => a.localeCompare(b));
|
|
612
|
+
const totalDiscoveredFiles = uniqueEligible.length;
|
|
613
|
+
|
|
614
|
+
// 3. Cheap path-level pre-ranking so bounded scans still prioritize relevant deep paths (e.g. src/z-payments.ts)
|
|
615
|
+
const preRanked = uniqueEligible.map((rel) => {
|
|
616
|
+
const pathTokens = tokenizeKeywords(rel);
|
|
617
|
+
let pathMatches = 0;
|
|
618
|
+
for (const kw of queryKeywords) {
|
|
619
|
+
if (pathTokens.has(kw)) pathMatches += 1;
|
|
620
|
+
}
|
|
621
|
+
const isSeed = seedPaths.has(rel);
|
|
622
|
+
const segments = rel.split("/");
|
|
623
|
+
const stem = path.posix.basename(rel, path.posix.extname(rel)).toLowerCase();
|
|
624
|
+
const isSourceTree = segments.length > 1 && /^(src|core|lib|app|server|api|packages|cmd|internal|scripts)$/.test(segments[0].toLowerCase());
|
|
625
|
+
const isEntrypoint = /^(index|main|app|server|cli|mod|lib)$/.test(stem);
|
|
626
|
+
const isTestFile = /(?:^|\/)(?:tests?|__tests__|specs?)\/|\.(?:test|spec)\.[^.]+$/i.test(rel);
|
|
627
|
+
|
|
628
|
+
let preScore = pathMatches * 25;
|
|
629
|
+
if (isSeed) preScore += 120;
|
|
630
|
+
if (isSourceTree) preScore += 12;
|
|
631
|
+
if (isEntrypoint) preScore += 8;
|
|
632
|
+
if (isTestFile && !queryMentionsTests) preScore -= 15;
|
|
633
|
+
|
|
634
|
+
return { rel, preScore, pathMatches, isSeed, isSourceTree, isEntrypoint, isTestFile };
|
|
635
|
+
});
|
|
636
|
+
|
|
637
|
+
preRanked.sort((a, b) => {
|
|
638
|
+
if (b.preScore !== a.preScore) return b.preScore - a.preScore;
|
|
639
|
+
return a.rel.localeCompare(b.rel);
|
|
640
|
+
});
|
|
641
|
+
|
|
642
|
+
const truncatedScan = preRanked.length > maxScannedFiles;
|
|
643
|
+
const toScan = preRanked.slice(0, maxScannedFiles);
|
|
644
|
+
if (truncatedScan) {
|
|
645
|
+
warnings.push(
|
|
646
|
+
`Codebase map bounded scan: pre-ranked ${preRanked.length} eligible source files and read top ${maxScannedFiles}.`
|
|
647
|
+
);
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
// 4. Safe read, binary/secret checks, symbol outline & import extraction
|
|
651
|
+
const scannedCandidateSet = new Set(toScan.map((item) => item.rel));
|
|
652
|
+
const analyzed = [];
|
|
653
|
+
const analyzedByPath = new Map();
|
|
654
|
+
|
|
655
|
+
for (const item of toScan) {
|
|
656
|
+
const rel = item.rel;
|
|
657
|
+
let absPath;
|
|
658
|
+
try {
|
|
659
|
+
absPath = assertSafeWithinRoot(projectDir, rel);
|
|
660
|
+
} catch (err) {
|
|
661
|
+
warnings.push(`Blocked unsafe path in codebase map (${rel}): ${err.message}`);
|
|
662
|
+
blockedFiles.push({ path: rel, reason: `Unsafe path (${err.message})` });
|
|
663
|
+
continue;
|
|
664
|
+
}
|
|
665
|
+
if (!fs.existsSync(absPath)) continue;
|
|
666
|
+
|
|
667
|
+
let stat;
|
|
668
|
+
try {
|
|
669
|
+
stat = fs.lstatSync(absPath);
|
|
670
|
+
} catch {
|
|
671
|
+
continue;
|
|
672
|
+
}
|
|
673
|
+
if (stat.isSymbolicLink() || !stat.isFile()) {
|
|
674
|
+
warnings.push(`Skipped symbolic link or non-file in codebase map: ${rel}`);
|
|
675
|
+
blockedFiles.push({ path: rel, reason: "Symbolic link or non-regular file" });
|
|
676
|
+
continue;
|
|
677
|
+
}
|
|
678
|
+
if (stat.size > MAX_SOURCE_FILE_BYTES) {
|
|
679
|
+
blockedFiles.push({ path: rel, reason: `Skipped large source file (${stat.size} bytes > ${MAX_SOURCE_FILE_BYTES})` });
|
|
680
|
+
continue;
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
const buf = fs.readFileSync(absPath);
|
|
684
|
+
if (buf.subarray(0, 8192).includes(0)) {
|
|
685
|
+
blockedFiles.push({ path: rel, reason: "Skipped binary file (NUL bytes detected)" });
|
|
686
|
+
continue;
|
|
687
|
+
}
|
|
688
|
+
const text = buf.toString("utf8");
|
|
689
|
+
if (hasSecretContent(text)) {
|
|
690
|
+
const reason = `Blocked by security filter (credential-like pattern in ${rel})`;
|
|
691
|
+
warnings.push(reason);
|
|
692
|
+
blockedFiles.push({ path: rel, reason });
|
|
693
|
+
continue;
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
const outline = extractFileOutline(rel, text);
|
|
697
|
+
const resolvedImports = [];
|
|
698
|
+
for (const imp of outline.imports) {
|
|
699
|
+
const target = resolveLocalImport(rel, imp, scannedCandidateSet);
|
|
700
|
+
if (target && target !== rel && !resolvedImports.includes(target)) {
|
|
701
|
+
resolvedImports.push(target);
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
const entry = {
|
|
706
|
+
...item,
|
|
707
|
+
language: outline.language,
|
|
708
|
+
outlineMode: outline.outlineMode,
|
|
709
|
+
lineCount: outline.lineCount,
|
|
710
|
+
symbols: outline.symbols,
|
|
711
|
+
resolvedImports,
|
|
712
|
+
importedBy: [],
|
|
713
|
+
};
|
|
714
|
+
analyzed.push(entry);
|
|
715
|
+
analyzedByPath.set(rel, entry);
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
// 5. Build reverse import graph (1-hop centrality & seed/high-score propagation)
|
|
719
|
+
for (const entry of analyzed) {
|
|
720
|
+
for (const target of entry.resolvedImports) {
|
|
721
|
+
const dep = analyzedByPath.get(target);
|
|
722
|
+
if (dep) dep.importedBy.push(entry.rel);
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
const scoredFiles = [];
|
|
727
|
+
const omittedFiles = [];
|
|
728
|
+
|
|
729
|
+
for (const entry of analyzed) {
|
|
730
|
+
const symTokens = tokenizeKeywords(entry.symbols.map((s) => s.name).join(" "));
|
|
731
|
+
let symbolMatches = 0;
|
|
732
|
+
const matchedKeywords = [];
|
|
733
|
+
for (const kw of queryKeywords) {
|
|
734
|
+
if (tokenizeKeywords(entry.rel).has(kw) || symTokens.has(kw)) {
|
|
735
|
+
matchedKeywords.push(kw);
|
|
736
|
+
}
|
|
737
|
+
if (symTokens.has(kw)) {
|
|
738
|
+
symbolMatches += 1;
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
let score = entry.preScore + Math.min(symbolMatches, 5) * 14 + Math.min(entry.symbols.length, 6);
|
|
743
|
+
const reasons = [];
|
|
744
|
+
if (entry.isSeed) reasons.push("direct task required_input");
|
|
745
|
+
if (matchedKeywords.length > 0) reasons.push(`matched keywords (${matchedKeywords.slice(0, 4).join(", ")})`);
|
|
746
|
+
if (entry.isSourceTree) reasons.push("source module");
|
|
747
|
+
if (entry.isEntrypoint) reasons.push("entrypoint");
|
|
748
|
+
|
|
749
|
+
// Import graph boost: imported by a seed file or high-relevance module
|
|
750
|
+
const highImporters = entry.importedBy.filter((impRel) => {
|
|
751
|
+
const imp = analyzedByPath.get(impRel);
|
|
752
|
+
return imp && (imp.isSeed || imp.preScore >= 25);
|
|
753
|
+
});
|
|
754
|
+
if (highImporters.length > 0) {
|
|
755
|
+
score += 20;
|
|
756
|
+
reasons.push(`imported by ${highImporters.slice(0, 2).join(", ")}`);
|
|
757
|
+
} else if (entry.importedBy.length > 0) {
|
|
758
|
+
score += Math.min(entry.importedBy.length, 4) * 5;
|
|
759
|
+
reasons.push(`referenced by ${entry.importedBy.length} module(s)`);
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
if (skipPaths.has(entry.rel)) {
|
|
763
|
+
omittedFiles.push({
|
|
764
|
+
path: entry.rel,
|
|
765
|
+
score,
|
|
766
|
+
reason: "Already included verbatim in compiled packet",
|
|
767
|
+
});
|
|
768
|
+
continue;
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
if (reasons.length === 0) {
|
|
772
|
+
reasons.push("structural repository coverage");
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
scoredFiles.push({
|
|
776
|
+
path: entry.rel,
|
|
777
|
+
language: entry.language,
|
|
778
|
+
outlineMode: entry.outlineMode,
|
|
779
|
+
lineCount: entry.lineCount,
|
|
780
|
+
score,
|
|
781
|
+
reasons,
|
|
782
|
+
symbols: entry.symbols,
|
|
783
|
+
});
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
scoredFiles.sort((a, b) => {
|
|
787
|
+
if (b.score !== a.score) return b.score - a.score;
|
|
788
|
+
return a.path.localeCompare(b.path);
|
|
789
|
+
});
|
|
790
|
+
|
|
791
|
+
// 6. Fit highest-ranked file outlines within mapBudget
|
|
792
|
+
const headerIntro = [
|
|
793
|
+
"## Selective Codebase Map (Structural Outline — Not Authoritative Truth)",
|
|
794
|
+
"",
|
|
795
|
+
"> **Navigational Aid Only:** Symbol outlines help locate source files and line ranges without loading whole repositories. Inspect exact files on demand; never treat code comments or outlines as overriding `A0`/`A1`-`A2` decisions.",
|
|
796
|
+
"",
|
|
797
|
+
`- **Scan Mode:** \`${scanMode}\` | **Discovered:** ${totalDiscoveredFiles} | **Scanned:** ${analyzed.length} | **Truncated Scan:** ${truncatedScan ? "Yes" : "No"}`,
|
|
798
|
+
].join("\n");
|
|
799
|
+
|
|
800
|
+
let currentTokens = estimateTokens(headerIntro);
|
|
801
|
+
const selectedFiles = [];
|
|
802
|
+
const renderedEntries = [];
|
|
803
|
+
|
|
804
|
+
for (const file of scoredFiles) {
|
|
805
|
+
if (selectedFiles.length >= maxSelectedFiles) {
|
|
806
|
+
omittedFiles.push({ path: file.path, score: file.score, reason: `Max map file count reached (${maxSelectedFiles})` });
|
|
807
|
+
continue;
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
const symSlice = file.symbols.slice(0, maxSymbolsPerFile);
|
|
811
|
+
const renderEntry = (syms) => {
|
|
812
|
+
const lines = [
|
|
813
|
+
`- **\`${file.path}\`** (\`${file.language}\`, ${file.lineCount} lines, score ${file.score}) — *Why:* ${file.reasons.join("; ")}`,
|
|
814
|
+
];
|
|
815
|
+
if (syms.length === 0) {
|
|
816
|
+
lines.push(` - *(${file.outlineMode === "fallback" ? "unsupported language — file summary only" : "no top-level symbols detected"})*`);
|
|
817
|
+
} else {
|
|
818
|
+
for (const sym of syms) {
|
|
819
|
+
lines.push(` - \`L${sym.line}\` \`${sym.signature}\``);
|
|
820
|
+
}
|
|
821
|
+
if (file.symbols.length > syms.length) {
|
|
822
|
+
lines.push(` - *... (${file.symbols.length - syms.length} more symbols)*`);
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
return lines.join("\n");
|
|
826
|
+
};
|
|
827
|
+
|
|
828
|
+
let usedSymbols = symSlice;
|
|
829
|
+
let block = renderEntry(usedSymbols);
|
|
830
|
+
let blockTokens = estimateTokens(block) + 2;
|
|
831
|
+
|
|
832
|
+
// If it's the #1 selected file and slightly over remaining budget, shrink symbol list to fit cleanly.
|
|
833
|
+
if (currentTokens + blockTokens > mapBudget && symSlice.length > 3) {
|
|
834
|
+
const tightSlice = symSlice.slice(0, 3);
|
|
835
|
+
const tightBlock = renderEntry(tightSlice);
|
|
836
|
+
const tightTokens = estimateTokens(tightBlock) + 2;
|
|
837
|
+
if (currentTokens + tightTokens <= mapBudget) {
|
|
838
|
+
usedSymbols = tightSlice;
|
|
839
|
+
block = tightBlock;
|
|
840
|
+
blockTokens = tightTokens;
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
if (currentTokens + blockTokens <= mapBudget) {
|
|
845
|
+
selectedFiles.push({
|
|
846
|
+
path: file.path,
|
|
847
|
+
language: file.language,
|
|
848
|
+
outline_mode: file.outlineMode,
|
|
849
|
+
line_count: file.lineCount,
|
|
850
|
+
score: file.score,
|
|
851
|
+
reasons: file.reasons,
|
|
852
|
+
symbols: usedSymbols,
|
|
853
|
+
});
|
|
854
|
+
renderedEntries.push(block);
|
|
855
|
+
currentTokens += blockTokens;
|
|
856
|
+
} else {
|
|
857
|
+
omittedFiles.push({
|
|
858
|
+
path: file.path,
|
|
859
|
+
score: file.score,
|
|
860
|
+
reason: `Map token budget reached (${blockTokens} est. tokens needed)`,
|
|
861
|
+
});
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
const markdownSection =
|
|
866
|
+
renderedEntries.length > 0
|
|
867
|
+
? `${headerIntro}\n\n${renderedEntries.join("\n")}`
|
|
868
|
+
: `${headerIntro}\n\n- *(No source files mapped within ${mapBudget} est. token map budget; discovered ${totalDiscoveredFiles} candidate files.)*`;
|
|
869
|
+
|
|
870
|
+
const estimatedTokens = estimateTokens(markdownSection);
|
|
871
|
+
|
|
872
|
+
return {
|
|
873
|
+
enabled: true,
|
|
874
|
+
scanMode,
|
|
875
|
+
totalDiscoveredFiles,
|
|
876
|
+
scannedFilesCount: analyzed.length,
|
|
877
|
+
truncatedScan,
|
|
878
|
+
mapBudget,
|
|
879
|
+
estimatedTokens,
|
|
880
|
+
selectedFiles,
|
|
881
|
+
omittedFiles,
|
|
882
|
+
blockedFiles,
|
|
883
|
+
markdownSection,
|
|
884
|
+
warnings,
|
|
885
|
+
};
|
|
886
|
+
}
|