@grundy40/contextslice 0.3.3 → 0.3.5
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/bin/ctxslice-mcp.mjs +343 -38
- package/bin/ctxslice.mjs +300 -13
- package/package.json +1 -1
package/bin/ctxslice-mcp.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
/**
|
|
3
|
-
* ctxslice-mcp.mjs — Global Multi-Language MCP Server v0.3.
|
|
3
|
+
* ctxslice-mcp.mjs — Global Multi-Language MCP Server v0.3.5
|
|
4
4
|
* Features: Embedded Base64 Tree-sitter WASM + Lazy Cache (~/.contextslice/parsers),
|
|
5
5
|
* State-Machine Brace Matcher Fallback, True Call Graph (BFS), TF-IDF, subPath Sandbox, mtime Cache
|
|
6
6
|
*/
|
|
@@ -11,7 +11,7 @@ import readline from 'node:readline';
|
|
|
11
11
|
import { createRequire } from 'node:module';
|
|
12
12
|
|
|
13
13
|
if (process.argv.includes('--version') || process.argv.includes('-v')) {
|
|
14
|
-
process.stdout.write('ctxslice-mcp v0.3.
|
|
14
|
+
process.stdout.write('ctxslice-mcp v0.3.5\n');
|
|
15
15
|
process.exit(0);
|
|
16
16
|
}
|
|
17
17
|
|
|
@@ -381,7 +381,7 @@ function expandQueryTerms(query) {
|
|
|
381
381
|
|
|
382
382
|
const TOOL_DEF = {
|
|
383
383
|
name: 'get_context_slice',
|
|
384
|
-
description: 'PRIMARY code-reading tool (v0.3.
|
|
384
|
+
description: 'PRIMARY code-reading tool (v0.3.5) with Tree-sitter WASM / State-Machine AST parsing, True Call Graph, TF-IDF & omitted summary.',
|
|
385
385
|
annotations: { readOnlyHint: true },
|
|
386
386
|
inputSchema: {
|
|
387
387
|
type: 'object',
|
|
@@ -781,17 +781,25 @@ function extractBlocks(code, ext, fileName) {
|
|
|
781
781
|
}
|
|
782
782
|
|
|
783
783
|
if (name) {
|
|
784
|
-
const
|
|
784
|
+
const isStmtOnly = kind === 'const' || (kind === 'type' && !line.includes('{'));
|
|
785
|
+
const end = isStmtOnly ? findStatementEndLine(lines, i, 'ts') : findBraceEnd(lines, i);
|
|
785
786
|
const full = lines.slice(i, end + 1).join('\n');
|
|
786
787
|
const sigIdx = full.indexOf('{');
|
|
787
|
-
const
|
|
788
|
+
const isShortConst = kind === 'const' && (end - i + 1) <= 10 && !full.includes('=>');
|
|
789
|
+
const skeleton = (kind === 'interface' || kind === 'type' || isShortConst || sigIdx === -1 || end - i < 4)
|
|
788
790
|
? full
|
|
789
791
|
: full.slice(0, sigIdx).trim() + ` { /* ... ${end - i - 1} lines folded */ }`;
|
|
790
|
-
blocks.push({ name, kind, startLine: i + 1, endLine: end + 1, full, skeleton });
|
|
792
|
+
blocks.push({ name, kind, exported: line.startsWith('export '), startLine: i + 1, endLine: end + 1, full, skeleton });
|
|
791
793
|
if (kind !== 'class' && kind !== 'module') i = end;
|
|
792
794
|
}
|
|
793
795
|
}
|
|
794
796
|
|
|
797
|
+
const topConsts = extractTopLevelConstants(code, e, blocks);
|
|
798
|
+
if (topConsts.length > 0) {
|
|
799
|
+
blocks.push(...topConsts);
|
|
800
|
+
blocks.sort((a, b) => a.startLine - b.startLine);
|
|
801
|
+
}
|
|
802
|
+
|
|
795
803
|
if (blocks.length === 0 && lines.length > 0) {
|
|
796
804
|
const name = path.basename(fileName || ('file' + e));
|
|
797
805
|
const full = code;
|
|
@@ -804,26 +812,7 @@ function extractBlocks(code, ext, fileName) {
|
|
|
804
812
|
return blocks;
|
|
805
813
|
}
|
|
806
814
|
|
|
807
|
-
async function collectProjectBlocks(root, scanRoot) {
|
|
808
|
-
const files = [];
|
|
809
|
-
const walk = (d) => {
|
|
810
|
-
if (!fs.existsSync(d)) return;
|
|
811
|
-
for (const e of fs.readdirSync(d, { withFileTypes: true })) {
|
|
812
|
-
if (IGNORE_DIRS.has(e.name)) continue;
|
|
813
|
-
const full = path.join(d, e.name);
|
|
814
|
-
if (e.isDirectory()) walk(full);
|
|
815
|
-
else if (isAcceptedFile(e.name)) files.push(full);
|
|
816
|
-
}
|
|
817
|
-
};
|
|
818
|
-
walk(scanRoot);
|
|
819
|
-
|
|
820
|
-
for (const cachedPath of FILE_CACHE.keys()) {
|
|
821
|
-
if (!fs.existsSync(cachedPath)) {
|
|
822
|
-
FILE_CACHE.delete(cachedPath);
|
|
823
|
-
}
|
|
824
|
-
}
|
|
825
815
|
|
|
826
|
-
|
|
827
816
|
function extractFileHeader(content, maxLines = 50, maxObjectLines = 60) {
|
|
828
817
|
if (!content) return null;
|
|
829
818
|
const lines = String(content).replace(/\r\n/g, '\n').split('\n');
|
|
@@ -954,8 +943,253 @@ function extractFileHeader(content, maxLines = 50, maxObjectLines = 60) {
|
|
|
954
943
|
}
|
|
955
944
|
|
|
956
945
|
|
|
946
|
+
function findStatementEndLine(lines, startIdx, mode) {
|
|
947
|
+
let parenDepth = 0;
|
|
948
|
+
let bracketDepth = 0;
|
|
949
|
+
let braceDepth = 0;
|
|
950
|
+
let inBlockComment = false;
|
|
951
|
+
let pyTripleQuote = null;
|
|
952
|
+
const maxScan = Math.min(lines.length - 1, startIdx + 120);
|
|
953
|
+
|
|
954
|
+
for (let j = startIdx; j <= maxScan; j++) {
|
|
955
|
+
const raw = lines[j];
|
|
956
|
+
const len = raw.length;
|
|
957
|
+
let k = 0;
|
|
958
|
+
while (k < len) {
|
|
959
|
+
const ch = raw[k];
|
|
960
|
+
const next = raw[k + 1];
|
|
961
|
+
const next3 = mode === 'py' ? raw.slice(k, k + 3) : '';
|
|
962
|
+
if (pyTripleQuote) {
|
|
963
|
+
if (next3 === pyTripleQuote) { pyTripleQuote = null; k += 3; continue; }
|
|
964
|
+
k++; continue;
|
|
965
|
+
}
|
|
966
|
+
if (inBlockComment) {
|
|
967
|
+
if (ch === '*' && next === '/') { inBlockComment = false; k += 2; continue; }
|
|
968
|
+
k++; continue;
|
|
969
|
+
}
|
|
970
|
+
if (mode === 'py' && ch === '#') break;
|
|
971
|
+
if (mode !== 'py' && ch === '/' && next === '/') break;
|
|
972
|
+
if (mode !== 'py' && ch === '/' && next === '*') { inBlockComment = true; k += 2; continue; }
|
|
973
|
+
if (mode === 'py' && (next3 === '"""' || next3 === "'''")) { pyTripleQuote = next3; k += 3; continue; }
|
|
974
|
+
if (ch === '"' || ch === "'" || (mode === 'ts' && ch === '\x60') || (mode === 'go' && ch === '\x60')) {
|
|
975
|
+
const quote = ch;
|
|
976
|
+
k++;
|
|
977
|
+
while (k < len) {
|
|
978
|
+
if (quote !== '\x60' && raw[k] === '\\') { k += 2; continue; }
|
|
979
|
+
if (raw[k] === quote) { k++; break; }
|
|
980
|
+
k++;
|
|
981
|
+
}
|
|
982
|
+
continue;
|
|
983
|
+
}
|
|
984
|
+
if (ch === '(') parenDepth++;
|
|
985
|
+
else if (ch === ')') parenDepth = Math.max(0, parenDepth - 1);
|
|
986
|
+
else if (ch === '[') bracketDepth++;
|
|
987
|
+
else if (ch === ']') bracketDepth = Math.max(0, bracketDepth - 1);
|
|
988
|
+
else if (ch === '{') braceDepth++;
|
|
989
|
+
else if (ch === '}') braceDepth = Math.max(0, braceDepth - 1);
|
|
990
|
+
k++;
|
|
991
|
+
}
|
|
992
|
+
if (!inBlockComment && !pyTripleQuote && parenDepth <= 0 && bracketDepth <= 0 && braceDepth <= 0) {
|
|
993
|
+
const trimmedEnd = raw.trimEnd();
|
|
994
|
+
if (trimmedEnd.endsWith('\\')) continue;
|
|
995
|
+
if (trimmedEnd.endsWith('=') && !trimmedEnd.endsWith('=>')) continue;
|
|
996
|
+
return j;
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
return maxScan;
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
function extractTopLevelConstants(code, ext, existingBlocks) {
|
|
1003
|
+
const e = (ext || '').toLowerCase();
|
|
1004
|
+
const lines = String(code || '').replace(/\r\n/g, '\n').split('\n');
|
|
1005
|
+
const covered = (existingBlocks || []).map((b) => [b.startLine, b.endLine]);
|
|
1006
|
+
const isCovered = (ln) => covered.some(([s, en]) => ln >= s && ln <= en);
|
|
1007
|
+
const out = [];
|
|
1008
|
+
|
|
1009
|
+
if (e === '.py' || e === '.pyw') {
|
|
1010
|
+
for (let i = 0; i < lines.length; i++) {
|
|
1011
|
+
const ln = i + 1;
|
|
1012
|
+
if (isCovered(ln)) continue;
|
|
1013
|
+
const line = lines[i];
|
|
1014
|
+
const m = line.match(/^([A-Z][A-Z0-9_]+|__all__|__version__)\s*(?::\s*[^=\n]+)?=\s*(?!=)/);
|
|
1015
|
+
if (!m) continue;
|
|
1016
|
+
const endIdx = findStatementEndLine(lines, i, 'py');
|
|
1017
|
+
const endLine = endIdx + 1;
|
|
1018
|
+
covered.push([ln, endLine]);
|
|
1019
|
+
const full = lines.slice(i, endLine).join('\n');
|
|
1020
|
+
const lineCount = endLine - ln + 1;
|
|
1021
|
+
const skeleton = lineCount <= 8 ? full : line + '\n # ... ' + (lineCount - 1) + ' lines folded';
|
|
1022
|
+
out.push({ name: m[1], kind: 'const', exported: !m[1].startsWith('_') || m[1] === '__all__', startLine: ln, endLine, full, skeleton });
|
|
1023
|
+
i = endIdx;
|
|
1024
|
+
}
|
|
1025
|
+
return out;
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
if (e === '.go') {
|
|
1029
|
+
for (let i = 0; i < lines.length; i++) {
|
|
1030
|
+
const ln = i + 1;
|
|
1031
|
+
if (isCovered(ln)) continue;
|
|
1032
|
+
const line = lines[i];
|
|
1033
|
+
const gm = line.match(/^(const|var)\s*\(/);
|
|
1034
|
+
if (gm) {
|
|
1035
|
+
const endIdx = findStatementEndLine(lines, i, 'go');
|
|
1036
|
+
const endLine = endIdx + 1;
|
|
1037
|
+
covered.push([ln, endLine]);
|
|
1038
|
+
const names = [];
|
|
1039
|
+
for (let j = i + 1; j <= endIdx; j++) {
|
|
1040
|
+
const im = lines[j].match(/^\s*([A-Za-z_][A-Za-z0-9_]*)\b/);
|
|
1041
|
+
if (im && im[1] !== '_' && im[1] !== 'iota') names.push(im[1]);
|
|
1042
|
+
}
|
|
1043
|
+
const name = names.length > 0 ? names.slice(0, 4).join(', ') : gm[1] + '_block';
|
|
1044
|
+
const full = lines.slice(i, endLine).join('\n');
|
|
1045
|
+
const lineCount = endLine - ln + 1;
|
|
1046
|
+
const skeleton = lineCount <= 12 ? full : line + '\n\t// ... ' + (lineCount - 2) + ' lines folded\n)';
|
|
1047
|
+
out.push({ name, kind: 'const', exported: names.some((n) => /^[A-Z]/.test(n)), startLine: ln, endLine, full, skeleton });
|
|
1048
|
+
i = endIdx;
|
|
1049
|
+
continue;
|
|
1050
|
+
}
|
|
1051
|
+
const sm = line.match(/^(const|var)\s+([A-Za-z_][A-Za-z0-9_]*)\b/);
|
|
1052
|
+
if (sm) {
|
|
1053
|
+
const endIdx = findStatementEndLine(lines, i, 'go');
|
|
1054
|
+
const endLine = endIdx + 1;
|
|
1055
|
+
covered.push([ln, endLine]);
|
|
1056
|
+
const full = lines.slice(i, endLine).join('\n');
|
|
1057
|
+
const lineCount = endLine - ln + 1;
|
|
1058
|
+
const skeleton = lineCount <= 8 ? full : line + '\n\t// ... ' + (lineCount - 1) + ' lines folded';
|
|
1059
|
+
out.push({ name: sm[2], kind: 'const', exported: /^[A-Z]/.test(sm[2]), startLine: ln, endLine, full, skeleton });
|
|
1060
|
+
i = endIdx;
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
return out;
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
if (e === '.rs') {
|
|
1067
|
+
for (let i = 0; i < lines.length; i++) {
|
|
1068
|
+
const ln = i + 1;
|
|
1069
|
+
if (isCovered(ln)) continue;
|
|
1070
|
+
const line = lines[i];
|
|
1071
|
+
const rm = line.match(/^(?:pub(?:\([^)]+\))?\s+)?(?:const|static)\s+([A-Z][A-Z0-9_]*)\b/);
|
|
1072
|
+
if (!rm) continue;
|
|
1073
|
+
const endIdx = findStatementEndLine(lines, i, 'rs');
|
|
1074
|
+
const endLine = endIdx + 1;
|
|
1075
|
+
covered.push([ln, endLine]);
|
|
1076
|
+
const full = lines.slice(i, endLine).join('\n');
|
|
1077
|
+
const lineCount = endLine - ln + 1;
|
|
1078
|
+
const skeleton = lineCount <= 8 ? full : line + '\n /* ... ' + (lineCount - 1) + ' lines folded */';
|
|
1079
|
+
out.push({ name: rm[1], kind: 'const', exported: line.startsWith('pub'), startLine: ln, endLine, full, skeleton });
|
|
1080
|
+
i = endIdx;
|
|
1081
|
+
}
|
|
1082
|
+
return out;
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
if (['.ts', '.tsx', '.mts', '.cts', '.js', '.jsx', '.mjs', '.cjs'].includes(e)) {
|
|
1086
|
+
for (let i = 0; i < lines.length; i++) {
|
|
1087
|
+
const ln = i + 1;
|
|
1088
|
+
if (isCovered(ln)) continue;
|
|
1089
|
+
const line = lines[i];
|
|
1090
|
+
let name = null;
|
|
1091
|
+
let exported = false;
|
|
1092
|
+
const cm = line.match(/^(export\s+)?(?:declare\s+)?(?:const|let|var)\s+([A-Za-z_$][A-Za-z0-9_$]*)\b/);
|
|
1093
|
+
if (cm) {
|
|
1094
|
+
exported = Boolean(cm[1]);
|
|
1095
|
+
if (exported || /^[A-Z][A-Z0-9_]+$/.test(cm[2])) name = cm[2];
|
|
1096
|
+
} else {
|
|
1097
|
+
const tm = line.match(/^(?:export\s+(default)\s+(?!(?:async\s+)?(?:function|class|abstract\s+class|interface)\b)|(module\.exports)\s*=)/);
|
|
1098
|
+
if (tm) { name = tm[1] ? 'default' : 'module.exports'; exported = true; }
|
|
1099
|
+
}
|
|
1100
|
+
if (!name) continue;
|
|
1101
|
+
const endIdx = findStatementEndLine(lines, i, 'ts');
|
|
1102
|
+
const endLine = endIdx + 1;
|
|
1103
|
+
const full = lines.slice(i, endLine).join('\n');
|
|
1104
|
+
if (full.includes('=>') && !name.startsWith('default')) continue;
|
|
1105
|
+
covered.push([ln, endLine]);
|
|
1106
|
+
const lineCount = endLine - ln + 1;
|
|
1107
|
+
const skeleton = lineCount <= 10 ? full : line + '\n /* ... ' + (lineCount - 1) + ' lines folded */';
|
|
1108
|
+
out.push({ name, kind: 'const', exported, startLine: ln, endLine, full, skeleton });
|
|
1109
|
+
i = endIdx;
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
return out;
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
function extractFileFooter(content, tailLines = 20) {
|
|
1116
|
+
if (!content) return null;
|
|
1117
|
+
const lines = String(content).replace(/\r\n/g, '\n').split('\n');
|
|
1118
|
+
let lastNonEmpty = lines.length - 1;
|
|
1119
|
+
while (lastNonEmpty >= 0 && lines[lastNonEmpty].trim() === '') lastNonEmpty--;
|
|
1120
|
+
if (lastNonEmpty < 0) return null;
|
|
1121
|
+
const windowStart = Math.max(0, lastNonEmpty - tailLines + 1);
|
|
1122
|
+
const stripFooterNoise = (line) =>
|
|
1123
|
+
line.replace(/(['"\x60])(?:\\.|(?!\1).)*\1/g, '""').replace(/\/\/.*$|#.*$/g, '');
|
|
1124
|
+
const findTailStmtEnd = (startIdx) => {
|
|
1125
|
+
let paren = 0, bracket = 0, brace = 0;
|
|
1126
|
+
for (let j = startIdx; j <= lastNonEmpty; j++) {
|
|
1127
|
+
const clean = stripFooterNoise(lines[j]);
|
|
1128
|
+
for (let k = 0; k < clean.length; k++) {
|
|
1129
|
+
const ch = clean[k];
|
|
1130
|
+
if (ch === '(') paren++;
|
|
1131
|
+
else if (ch === ')') paren = Math.max(0, paren - 1);
|
|
1132
|
+
else if (ch === '[') bracket++;
|
|
1133
|
+
else if (ch === ']') bracket = Math.max(0, bracket - 1);
|
|
1134
|
+
else if (ch === '{') brace++;
|
|
1135
|
+
else if (ch === '}') brace = Math.max(0, brace - 1);
|
|
1136
|
+
}
|
|
1137
|
+
if (paren <= 0 && bracket <= 0 && brace <= 0) {
|
|
1138
|
+
const t = lines[j].trimEnd();
|
|
1139
|
+
if (t.endsWith('\\') || (t.endsWith('=') && !t.endsWith('=>'))) continue;
|
|
1140
|
+
return j;
|
|
1141
|
+
}
|
|
1142
|
+
}
|
|
1143
|
+
return lastNonEmpty;
|
|
1144
|
+
};
|
|
1145
|
+
const footerStartRe =
|
|
1146
|
+
/^(?:export\s+default\s+(?!(?:async\s+)?(?:function|class|abstract\s+class|interface)\b)|export\s+(?:type\s+)?\{|export\s+\*\s+from\b|export\s+(?:const|let|var)\s+|module\.exports\s*=|exports\.[A-Za-z_$][A-Za-z0-9_$]*\s*=|(?:pub(?:\([^)]+\))?\s+)?(?:const|static)\s+[A-Za-z_][A-Za-z0-9_]*|(?:const|var)\s*\(|(?:const|var)\s+[A-Z][A-Za-z0-9_]*\b|(?:[A-Z][A-Z0-9_]+|__all__|__version__)\s*(?::\s*[^=\n]+)?=\s*(?!=))/;
|
|
1147
|
+
const chunks = [];
|
|
1148
|
+
let i = windowStart;
|
|
1149
|
+
while (i <= lastNonEmpty) {
|
|
1150
|
+
const raw = lines[i];
|
|
1151
|
+
if (!raw || /^\s/.test(raw)) { i++; continue; }
|
|
1152
|
+
const trimmed = raw.trim();
|
|
1153
|
+
if (trimmed.startsWith('//') || trimmed.startsWith('#') || trimmed.startsWith('/*') || !footerStartRe.test(raw)) {
|
|
1154
|
+
i++; continue;
|
|
1155
|
+
}
|
|
1156
|
+
const endIdx = findTailStmtEnd(i);
|
|
1157
|
+
chunks.push({ startLine: i + 1, endLine: endIdx + 1, text: lines.slice(i, endIdx + 1).join('\n') });
|
|
1158
|
+
i = endIdx + 1;
|
|
1159
|
+
}
|
|
1160
|
+
if (chunks.length === 0) return null;
|
|
1161
|
+
return {
|
|
1162
|
+
footer: chunks.map((c) => c.text).join('\n'),
|
|
1163
|
+
startLine: chunks[0].startLine,
|
|
1164
|
+
endLine: chunks[chunks.length - 1].endLine,
|
|
1165
|
+
chunks
|
|
1166
|
+
};
|
|
1167
|
+
}
|
|
1168
|
+
|
|
1169
|
+
|
|
1170
|
+
async function collectProjectBlocks(root, scanRoot) {
|
|
1171
|
+
const files = [];
|
|
1172
|
+
const walk = (d) => {
|
|
1173
|
+
if (!fs.existsSync(d)) return;
|
|
1174
|
+
for (const e of fs.readdirSync(d, { withFileTypes: true })) {
|
|
1175
|
+
if (IGNORE_DIRS.has(e.name)) continue;
|
|
1176
|
+
const full = path.join(d, e.name);
|
|
1177
|
+
if (e.isDirectory()) walk(full);
|
|
1178
|
+
else if (isAcceptedFile(e.name)) files.push(full);
|
|
1179
|
+
}
|
|
1180
|
+
};
|
|
1181
|
+
walk(scanRoot);
|
|
1182
|
+
|
|
1183
|
+
for (const cachedPath of FILE_CACHE.keys()) {
|
|
1184
|
+
if (!fs.existsSync(cachedPath)) {
|
|
1185
|
+
FILE_CACHE.delete(cachedPath);
|
|
1186
|
+
}
|
|
1187
|
+
}
|
|
1188
|
+
|
|
957
1189
|
const allBlocks = [];
|
|
958
1190
|
const headerMap = new Map();
|
|
1191
|
+
const footerMap = new Map();
|
|
1192
|
+
const lineCountMap = new Map();
|
|
959
1193
|
for (const f of files) {
|
|
960
1194
|
const rel = path.relative(root, f);
|
|
961
1195
|
let stat;
|
|
@@ -970,27 +1204,36 @@ function extractFileHeader(content, maxLines = 50, maxObjectLines = 60) {
|
|
|
970
1204
|
let items;
|
|
971
1205
|
let parserMode = 'regex';
|
|
972
1206
|
let fileHeader = null;
|
|
1207
|
+
let fileFooter = null;
|
|
1208
|
+
let fileLines = 0;
|
|
973
1209
|
if (cached && cached.mtime === mtime) {
|
|
974
1210
|
items = cached.items;
|
|
975
1211
|
parserMode = cached.parserMode || 'regex';
|
|
976
1212
|
fileHeader = cached.header ?? null;
|
|
1213
|
+
fileFooter = cached.footer ?? null;
|
|
1214
|
+
fileLines = cached.lineCount || 0;
|
|
977
1215
|
} else {
|
|
978
1216
|
const rawBuf = fs.readFileSync(f);
|
|
979
1217
|
if (isBinaryBuffer(rawBuf)) continue;
|
|
980
1218
|
const raw = rawBuf.toString('utf8');
|
|
1219
|
+
fileLines = raw.split('\n').length;
|
|
981
1220
|
fileHeader = extractFileHeader(raw);
|
|
1221
|
+
fileFooter = extractFileFooter(raw);
|
|
982
1222
|
const ext = path.extname(f);
|
|
983
1223
|
const tsBlocks = await extractBlocksWithTreeSitter(raw, ext, rel);
|
|
984
1224
|
if (tsBlocks && tsBlocks.length > 0) {
|
|
985
|
-
|
|
1225
|
+
const extraConsts = extractTopLevelConstants(raw, ext, tsBlocks);
|
|
1226
|
+
items = extraConsts.length > 0 ? [...tsBlocks, ...extraConsts].sort((a, b) => a.startLine - b.startLine) : tsBlocks;
|
|
986
1227
|
parserMode = 'tree-sitter-wasm';
|
|
987
1228
|
} else {
|
|
988
1229
|
items = extractBlocks(raw, ext, rel);
|
|
989
1230
|
parserMode = 'state-machine-regex';
|
|
990
1231
|
}
|
|
991
|
-
FILE_CACHE.set(f, { mtime, parserMode, items, header: fileHeader });
|
|
1232
|
+
FILE_CACHE.set(f, { mtime, parserMode, items, header: fileHeader, footer: fileFooter, lineCount: fileLines });
|
|
992
1233
|
}
|
|
993
1234
|
if (fileHeader) headerMap.set(rel, fileHeader);
|
|
1235
|
+
if (fileFooter) footerMap.set(rel, fileFooter);
|
|
1236
|
+
lineCountMap.set(rel, fileLines);
|
|
994
1237
|
|
|
995
1238
|
for (const b of items) {
|
|
996
1239
|
allBlocks.push({
|
|
@@ -1003,7 +1246,7 @@ function extractFileHeader(content, maxLines = 50, maxObjectLines = 60) {
|
|
|
1003
1246
|
});
|
|
1004
1247
|
}
|
|
1005
1248
|
}
|
|
1006
|
-
return { blocks: allBlocks, headerMap };
|
|
1249
|
+
return { blocks: allBlocks, headerMap, footerMap, lineCountMap };
|
|
1007
1250
|
}
|
|
1008
1251
|
|
|
1009
1252
|
function resolveScanRoot(projectPath, subPath) {
|
|
@@ -1027,16 +1270,19 @@ async function expandSkeletonCluster(symbols, projectPath, budget = 32768, subPa
|
|
|
1027
1270
|
try {
|
|
1028
1271
|
const { root, scanRoot } = resolveScanRoot(projectPath, subPath);
|
|
1029
1272
|
const wanted = new Set((symbols || []).map((s) => String(s).toLowerCase()));
|
|
1030
|
-
const { blocks, headerMap } = await collectProjectBlocks(root, scanRoot);
|
|
1273
|
+
const { blocks, headerMap, footerMap, lineCountMap } = await collectProjectBlocks(root, scanRoot);
|
|
1031
1274
|
let used = 0;
|
|
1032
1275
|
const expanded = [];
|
|
1033
1276
|
const expandedFiles = new Set();
|
|
1034
1277
|
const fullStart1EndLine = new Map();
|
|
1278
|
+
const coveredRangesByFile = new Map();
|
|
1035
1279
|
for (const b of blocks) {
|
|
1036
1280
|
const match = wanted.has(b.name.toLowerCase()) || [...wanted].some((w) => b.name.toLowerCase().includes(w) || b.rel.toLowerCase().includes(w));
|
|
1037
1281
|
if (match && used + b.rawTk <= budget) {
|
|
1038
1282
|
used += b.rawTk;
|
|
1039
1283
|
expandedFiles.add(b.rel);
|
|
1284
|
+
if (!coveredRangesByFile.has(b.rel)) coveredRangesByFile.set(b.rel, []);
|
|
1285
|
+
coveredRangesByFile.get(b.rel).push([b.startLine, b.endLine]);
|
|
1040
1286
|
if (b.startLine === 1) {
|
|
1041
1287
|
fullStart1EndLine.set(b.rel, Math.max(fullStart1EndLine.get(b.rel) || 0, b.endLine));
|
|
1042
1288
|
}
|
|
@@ -1044,14 +1290,30 @@ async function expandSkeletonCluster(symbols, projectPath, budget = 32768, subPa
|
|
|
1044
1290
|
}
|
|
1045
1291
|
}
|
|
1046
1292
|
const headerChunks = [];
|
|
1293
|
+
const headerEndByFile = new Map();
|
|
1047
1294
|
for (const rel of expandedFiles) {
|
|
1048
1295
|
const hdr = headerMap.get(rel);
|
|
1049
1296
|
if (!hdr) continue;
|
|
1050
1297
|
if ((fullStart1EndLine.get(rel) || 0) >= hdr.endLine) continue;
|
|
1298
|
+
headerEndByFile.set(rel, hdr.endLine);
|
|
1051
1299
|
headerChunks.push(`// [${rel}:1-${hdr.endLine}] (file header)\n${hdr.header}`);
|
|
1052
1300
|
}
|
|
1301
|
+
const footerChunks = [];
|
|
1302
|
+
for (const rel of expandedFiles) {
|
|
1303
|
+
const ftr = footerMap.get(rel);
|
|
1304
|
+
if (!ftr) continue;
|
|
1305
|
+
const ranges = coveredRangesByFile.get(rel) || [];
|
|
1306
|
+
const totalLines = lineCountMap.get(rel) || 0;
|
|
1307
|
+
if (ranges.some(([s, e]) => s <= 1 && e >= totalLines)) continue;
|
|
1308
|
+
const hdrEnd = headerEndByFile.get(rel) || 0;
|
|
1309
|
+
const uncovered = ftr.chunks.filter((c) => c.startLine > hdrEnd && !ranges.some(([s, e]) => c.startLine >= s && c.endLine <= e));
|
|
1310
|
+
if (uncovered.length > 0) {
|
|
1311
|
+
footerChunks.push(`// [${rel}:${uncovered[0].startLine}-${uncovered[uncovered.length - 1].endLine}] (file footer)\n${uncovered.map((c) => c.text).join('\n')}`);
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1053
1314
|
const hdrTag = headerChunks.length > 0 ? `\n<file_headers>\n${headerChunks.join('\n\n')}\n</file_headers>` : '';
|
|
1054
|
-
|
|
1315
|
+
const ftrTag = footerChunks.length > 0 ? `\n<file_footers>\n${footerChunks.join('\n\n')}\n</file_footers>` : '';
|
|
1316
|
+
return `<skeleton_expansion root="${root}" subPath="${subPath || ''}" expanded="${expanded.length}" used="${used}">${hdrTag}\n${expanded.join('\n\n')}${ftrTag}\n</skeleton_expansion>`;
|
|
1055
1317
|
} catch (err) {
|
|
1056
1318
|
return `Error: ${err.message}`;
|
|
1057
1319
|
}
|
|
@@ -1061,7 +1323,7 @@ async function sliceProject(query, projectPath, budget = 32768, autoExpand = tru
|
|
|
1061
1323
|
try {
|
|
1062
1324
|
const { root, scanRoot } = resolveScanRoot(projectPath, subPath);
|
|
1063
1325
|
const terms = expandQueryTerms(String(query));
|
|
1064
|
-
const { blocks, headerMap } = await collectProjectBlocks(root, scanRoot);
|
|
1326
|
+
const { blocks, headerMap, footerMap, lineCountMap } = await collectProjectBlocks(root, scanRoot);
|
|
1065
1327
|
const totalBlocks = Math.max(1, blocks.length);
|
|
1066
1328
|
const termDF = {};
|
|
1067
1329
|
for (const t of terms) {
|
|
@@ -1091,13 +1353,20 @@ async function sliceProject(query, projectPath, budget = 32768, autoExpand = tru
|
|
|
1091
1353
|
return { ...b, score };
|
|
1092
1354
|
}).sort((a, b) => b.score - a.score);
|
|
1093
1355
|
|
|
1094
|
-
// 構建專案 SymbolMap
|
|
1356
|
+
// 構建專案 SymbolMap 與真實依賴圖 (True Call Graph + Constants)
|
|
1095
1357
|
const symbolMap = new Map();
|
|
1358
|
+
const constSymbolNames = new Set();
|
|
1096
1359
|
for (let idx = 0; idx < allBlocks.length; idx++) {
|
|
1097
1360
|
const b = allBlocks[idx];
|
|
1098
1361
|
if (b.name && b.kind !== 'module' && !CONTROL_WORDS.has(b.name)) {
|
|
1099
|
-
|
|
1100
|
-
|
|
1362
|
+
const names = (b.kind === 'const' && b.name.includes(','))
|
|
1363
|
+
? b.name.split(',').map((s) => s.trim()).filter(Boolean)
|
|
1364
|
+
: [b.name];
|
|
1365
|
+
for (const sn of names) {
|
|
1366
|
+
if (!symbolMap.has(sn)) symbolMap.set(sn, []);
|
|
1367
|
+
symbolMap.get(sn).push(idx);
|
|
1368
|
+
if (b.kind === 'const') constSymbolNames.add(sn);
|
|
1369
|
+
}
|
|
1101
1370
|
}
|
|
1102
1371
|
}
|
|
1103
1372
|
|
|
@@ -1110,7 +1379,8 @@ async function sliceProject(query, projectPath, budget = 32768, autoExpand = tru
|
|
|
1110
1379
|
if (sym === caller.name) continue;
|
|
1111
1380
|
if (maskedCaller.includes(sym)) {
|
|
1112
1381
|
const esc = sym.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
1113
|
-
const
|
|
1382
|
+
const isConstRef = constSymbolNames.has(sym) && sym.length >= 3;
|
|
1383
|
+
const callRe = isConstRef ? new RegExp('\\b' + esc + '\\b', 'm') : new RegExp('\\b' + esc + '\\s*\\(', 'm');
|
|
1114
1384
|
if (callRe.test(maskedCaller)) {
|
|
1115
1385
|
for (const t of (symbolMap.get(sym) || [])) {
|
|
1116
1386
|
callAdj[i].add(t);
|
|
@@ -1123,11 +1393,13 @@ async function sliceProject(query, projectPath, budget = 32768, autoExpand = tru
|
|
|
1123
1393
|
|
|
1124
1394
|
// 計算自頂部修改重點 (Focal) 出發的 1 層 (1-hop) 與 2 層 (2-hop) 關聯距離
|
|
1125
1395
|
const focalIndices = [];
|
|
1396
|
+
const focalFiles = new Set();
|
|
1126
1397
|
const hopDistances = new Map();
|
|
1127
1398
|
for (let i = 0; i < allBlocks.length; i++) {
|
|
1128
1399
|
if (allBlocks[i].score > 0 && focalIndices.length < 3) {
|
|
1129
1400
|
focalIndices.push(i);
|
|
1130
1401
|
hopDistances.set(i, 0);
|
|
1402
|
+
focalFiles.add(allBlocks[i].rel);
|
|
1131
1403
|
}
|
|
1132
1404
|
}
|
|
1133
1405
|
|
|
@@ -1151,7 +1423,13 @@ async function sliceProject(query, projectPath, budget = 32768, autoExpand = tru
|
|
|
1151
1423
|
const focalNames = new Set();
|
|
1152
1424
|
const includedIds = new Set();
|
|
1153
1425
|
const includedFiles = new Set();
|
|
1426
|
+
const partialFiles = new Set();
|
|
1154
1427
|
const fullStart1EndLine = new Map();
|
|
1428
|
+
const coveredRangesByFile = new Map();
|
|
1429
|
+
const addCoveredRange = (rel, s, e) => {
|
|
1430
|
+
if (!coveredRangesByFile.has(rel)) coveredRangesByFile.set(rel, []);
|
|
1431
|
+
coveredRangesByFile.get(rel).push([s, e]);
|
|
1432
|
+
};
|
|
1155
1433
|
const candidateItems = [];
|
|
1156
1434
|
const dirBudgetUsed = {};
|
|
1157
1435
|
const maxDirBudget = budget * 0.65;
|
|
@@ -1169,22 +1447,30 @@ async function sliceProject(query, projectPath, budget = 32768, autoExpand = tru
|
|
|
1169
1447
|
focalNames.add(b.name);
|
|
1170
1448
|
includedIds.add(b.rel + ':' + b.name + '@' + b.startLine);
|
|
1171
1449
|
includedFiles.add(b.rel);
|
|
1450
|
+
addCoveredRange(b.rel, b.startLine, b.endLine);
|
|
1172
1451
|
if (b.startLine === 1) {
|
|
1173
1452
|
fullStart1EndLine.set(b.rel, Math.max(fullStart1EndLine.get(b.rel) || 0, b.endLine));
|
|
1174
1453
|
}
|
|
1175
1454
|
fullChunks.push(`// [${b.rel}:${b.startLine}-${b.endLine}] [${b.parserMode}]\n${b.full}`);
|
|
1176
1455
|
} else if (used + b.skelTk <= budget && curDir + b.skelTk <= maxDirBudget) {
|
|
1177
|
-
const
|
|
1456
|
+
const sameFileConst = b.kind === 'const' && (b.exported || /^[A-Z][A-Z0-9_]+$/.test(b.name)) && b.lineCount <= 25 && focalFiles.has(b.rel);
|
|
1457
|
+
const linked = (hop !== undefined && hop <= 2) || b.score > 0 || sameFileConst;
|
|
1178
1458
|
if (linked || i < 12) {
|
|
1179
1459
|
used += b.skelTk;
|
|
1180
1460
|
dirBudgetUsed[topDir] = curDir + b.skelTk;
|
|
1181
1461
|
skelCandidates.push({ ...b, linked, hop: hop ?? null });
|
|
1182
1462
|
includedIds.add(b.rel + ':' + b.name + '@' + b.startLine);
|
|
1183
1463
|
includedFiles.add(b.rel);
|
|
1464
|
+
partialFiles.add(b.rel);
|
|
1465
|
+
if (b.skeleton === b.full) {
|
|
1466
|
+
addCoveredRange(b.rel, b.startLine, b.endLine);
|
|
1467
|
+
}
|
|
1184
1468
|
} else {
|
|
1469
|
+
partialFiles.add(b.rel);
|
|
1185
1470
|
candidateItems.push(b);
|
|
1186
1471
|
}
|
|
1187
1472
|
} else {
|
|
1473
|
+
partialFiles.add(b.rel);
|
|
1188
1474
|
candidateItems.push(b);
|
|
1189
1475
|
}
|
|
1190
1476
|
}
|
|
@@ -1201,6 +1487,7 @@ async function sliceProject(query, projectPath, budget = 32768, autoExpand = tru
|
|
|
1201
1487
|
used += delta;
|
|
1202
1488
|
dirBudgetUsed[topDir] = curDir + delta;
|
|
1203
1489
|
autoExpandedNames.push(item.name);
|
|
1490
|
+
addCoveredRange(item.rel, item.startLine, item.endLine);
|
|
1204
1491
|
if (item.startLine === 1) {
|
|
1205
1492
|
fullStart1EndLine.set(item.rel, Math.max(fullStart1EndLine.get(item.rel) || 0, item.endLine));
|
|
1206
1493
|
}
|
|
@@ -1212,14 +1499,32 @@ async function sliceProject(query, projectPath, budget = 32768, autoExpand = tru
|
|
|
1212
1499
|
}
|
|
1213
1500
|
|
|
1214
1501
|
const headerChunks = [];
|
|
1502
|
+
const headerEndByFile = new Map();
|
|
1215
1503
|
for (const rel of includedFiles) {
|
|
1216
1504
|
const hdr = headerMap.get(rel);
|
|
1217
1505
|
if (!hdr) continue;
|
|
1218
1506
|
if ((fullStart1EndLine.get(rel) || 0) >= hdr.endLine) continue;
|
|
1507
|
+
headerEndByFile.set(rel, hdr.endLine);
|
|
1219
1508
|
headerChunks.push(`// [${rel}:1-${hdr.endLine}] (file header)\n${hdr.header}`);
|
|
1220
1509
|
}
|
|
1221
1510
|
const headerTag = headerChunks.length > 0 ? `\n<file_headers>\n${headerChunks.join('\n\n')}\n</file_headers>` : '';
|
|
1222
1511
|
|
|
1512
|
+
const footerChunks = [];
|
|
1513
|
+
for (const rel of includedFiles) {
|
|
1514
|
+
const ftr = footerMap.get(rel);
|
|
1515
|
+
if (!ftr) continue;
|
|
1516
|
+
const ranges = coveredRangesByFile.get(rel) || [];
|
|
1517
|
+
const totalLines = lineCountMap.get(rel) || 0;
|
|
1518
|
+
const wholeFileCovered = ranges.some(([s, e]) => s <= 1 && e >= totalLines);
|
|
1519
|
+
if (!partialFiles.has(rel) && wholeFileCovered) continue;
|
|
1520
|
+
const hdrEnd = headerEndByFile.get(rel) || 0;
|
|
1521
|
+
const uncovered = ftr.chunks.filter((c) => c.startLine > hdrEnd && !ranges.some(([s, e]) => c.startLine >= s && c.endLine <= e));
|
|
1522
|
+
if (uncovered.length > 0) {
|
|
1523
|
+
footerChunks.push(`// [${rel}:${uncovered[0].startLine}-${uncovered[uncovered.length - 1].endLine}] (file footer)\n${uncovered.map((c) => c.text).join('\n')}`);
|
|
1524
|
+
}
|
|
1525
|
+
}
|
|
1526
|
+
const footerTag = footerChunks.length > 0 ? `\n<file_footers>\n${footerChunks.join('\n\n')}\n</file_footers>` : '';
|
|
1527
|
+
|
|
1223
1528
|
const omitted = candidateItems.filter(b => b.score > 0 && !includedIds.has(b.rel + ':' + b.name + '@' + b.startLine)).slice(0, 3);
|
|
1224
1529
|
const omittedTag = omitted.length > 0
|
|
1225
1530
|
? `\n<omitted_top_matches>\n` + omitted.map(o => ` <!-- Omitted: ${o.rel}:${o.startLine} (${o.name}, score: ${o.score.toFixed(1)}) — consider expanding -->`).join('\n') + `\n</omitted_top_matches>`
|
|
@@ -1227,7 +1532,7 @@ async function sliceProject(query, projectPath, budget = 32768, autoExpand = tru
|
|
|
1227
1532
|
|
|
1228
1533
|
const expandTag = autoExpandedNames.length > 0 ? ` autoExpanded="${autoExpandedNames.join(',')}"` : '';
|
|
1229
1534
|
const subPathTag = subPath ? ` subPath="${subPath}"` : '';
|
|
1230
|
-
return `<context_slice root="${root}" version="0.3.
|
|
1535
|
+
return `<context_slice root="${root}" version="0.3.5"${subPathTag} budget="${budget}" used="${used}"${expandTag}>${headerTag}\n<skeletons>\n${finalSkelChunks.join('\n')}\n</skeletons>\n<focal>\n${fullChunks.join('\n\n')}\n</focal>${footerTag}${omittedTag}\n</context_slice>`;
|
|
1231
1536
|
} catch (err) {
|
|
1232
1537
|
return `Error: ${err.message}`;
|
|
1233
1538
|
}
|
|
@@ -1246,7 +1551,7 @@ rl.on('line', async (line) => {
|
|
|
1246
1551
|
return send({
|
|
1247
1552
|
protocolVersion: req.params?.protocolVersion || '2024-11-05',
|
|
1248
1553
|
capabilities: { tools: {} },
|
|
1249
|
-
serverInfo: { name: 'ctxslice-thin', version: '0.3.
|
|
1554
|
+
serverInfo: { name: 'ctxslice-thin', version: '0.3.5' }
|
|
1250
1555
|
});
|
|
1251
1556
|
}
|
|
1252
1557
|
if (req.method === 'ping') return send({});
|
package/bin/ctxslice.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
/**
|
|
3
|
-
* ctxslice.mjs — Zero-Config Multi-Language Local AST Context Pre-Compiler (v0.3.
|
|
3
|
+
* ctxslice.mjs — Zero-Config Multi-Language Local AST Context Pre-Compiler (v0.3.5)
|
|
4
4
|
* Features: Multi-Language AST Extractors, State-Machine Brace Matcher, True Call Graph (BFS), TF-IDF, Directory Quota Cap
|
|
5
5
|
*/
|
|
6
6
|
import fs from 'node:fs';
|
|
@@ -372,12 +372,12 @@ function expandQueryTerms(query) {
|
|
|
372
372
|
|
|
373
373
|
const args = process.argv.slice(2);
|
|
374
374
|
if (args.includes('--version') || args.includes('-v')) {
|
|
375
|
-
process.stdout.write('ctxslice v0.3.
|
|
375
|
+
process.stdout.write('ctxslice v0.3.5\n');
|
|
376
376
|
process.exit(0);
|
|
377
377
|
}
|
|
378
378
|
if (args.includes('--help') || args.includes('-h')) {
|
|
379
379
|
process.stdout.write(
|
|
380
|
-
'ContextSlice CLI v0.3.
|
|
380
|
+
'ContextSlice CLI v0.3.5 — Zero-Config Multi-Language AST Slicer\n\n' +
|
|
381
381
|
'Usage:\n ctxslice "<query>" [--root <dir>] [--budget <tokens>]\n\n' +
|
|
382
382
|
'Options:\n --root <dir> Target repository path (default: current directory)\n' +
|
|
383
383
|
' --budget <num> Max token budget (default: 32768)\n' +
|
|
@@ -437,9 +437,13 @@ function walk(dir, acc = []) {
|
|
|
437
437
|
}
|
|
438
438
|
|
|
439
439
|
function estimateTokens(str) {
|
|
440
|
-
|
|
440
|
+
if (!str) return 0;
|
|
441
|
+
let cjk = 0;
|
|
442
|
+
for (let i = 0; i < str.length; i++) if (str.charCodeAt(i) >= 128) cjk++;
|
|
443
|
+
return Math.max(1, Math.round((str.length - cjk) / 2.1 + cjk * 1.6));
|
|
441
444
|
}
|
|
442
445
|
|
|
446
|
+
|
|
443
447
|
function maskCommentsAndStrings(code) {
|
|
444
448
|
return code
|
|
445
449
|
.replace(/\/\*[\s\S]*?\*\//g, (m) => m.replace(/[^\n]/g, ' '))
|
|
@@ -577,17 +581,25 @@ function extractBlocks(code, ext, fileName) {
|
|
|
577
581
|
}
|
|
578
582
|
|
|
579
583
|
if (name) {
|
|
580
|
-
const
|
|
584
|
+
const isStmtOnly = kind === 'const' || (kind === 'type' && !line.includes('{'));
|
|
585
|
+
const end = isStmtOnly ? findStatementEndLine(lines, i, 'ts') : findBraceEnd(lines, i);
|
|
581
586
|
const full = lines.slice(i, end + 1).join('\n');
|
|
582
587
|
const sigIdx = full.indexOf('{');
|
|
583
|
-
const
|
|
588
|
+
const isShortConst = kind === 'const' && (end - i + 1) <= 10 && !full.includes('=>');
|
|
589
|
+
const skeleton = (kind === 'interface' || kind === 'type' || isShortConst || sigIdx === -1 || end - i < 4)
|
|
584
590
|
? full
|
|
585
591
|
: full.slice(0, sigIdx).trim() + ` { /* ... ${end - i - 1} lines folded */ }`;
|
|
586
|
-
blocks.push({ name, kind, startLine: i + 1, endLine: end + 1, full, skeleton });
|
|
592
|
+
blocks.push({ name, kind, exported: line.startsWith('export '), startLine: i + 1, endLine: end + 1, full, skeleton });
|
|
587
593
|
if (kind !== 'class' && kind !== 'module') i = end;
|
|
588
594
|
}
|
|
589
595
|
}
|
|
590
596
|
|
|
597
|
+
const topConsts = extractTopLevelConstants(code, e, blocks);
|
|
598
|
+
if (topConsts.length > 0) {
|
|
599
|
+
blocks.push(...topConsts);
|
|
600
|
+
blocks.sort((a, b) => a.startLine - b.startLine);
|
|
601
|
+
}
|
|
602
|
+
|
|
591
603
|
if (blocks.length === 0 && lines.length > 0) {
|
|
592
604
|
const name = path.basename(fileName || ('file' + e));
|
|
593
605
|
const full = code;
|
|
@@ -728,18 +740,247 @@ function extractFileHeader(content, maxLines = 50, maxObjectLines = 60) {
|
|
|
728
740
|
}
|
|
729
741
|
|
|
730
742
|
|
|
743
|
+
function findStatementEndLine(lines, startIdx, mode) {
|
|
744
|
+
let parenDepth = 0;
|
|
745
|
+
let bracketDepth = 0;
|
|
746
|
+
let braceDepth = 0;
|
|
747
|
+
let inBlockComment = false;
|
|
748
|
+
let pyTripleQuote = null;
|
|
749
|
+
const maxScan = Math.min(lines.length - 1, startIdx + 120);
|
|
750
|
+
|
|
751
|
+
for (let j = startIdx; j <= maxScan; j++) {
|
|
752
|
+
const raw = lines[j];
|
|
753
|
+
const len = raw.length;
|
|
754
|
+
let k = 0;
|
|
755
|
+
while (k < len) {
|
|
756
|
+
const ch = raw[k];
|
|
757
|
+
const next = raw[k + 1];
|
|
758
|
+
const next3 = mode === 'py' ? raw.slice(k, k + 3) : '';
|
|
759
|
+
if (pyTripleQuote) {
|
|
760
|
+
if (next3 === pyTripleQuote) { pyTripleQuote = null; k += 3; continue; }
|
|
761
|
+
k++; continue;
|
|
762
|
+
}
|
|
763
|
+
if (inBlockComment) {
|
|
764
|
+
if (ch === '*' && next === '/') { inBlockComment = false; k += 2; continue; }
|
|
765
|
+
k++; continue;
|
|
766
|
+
}
|
|
767
|
+
if (mode === 'py' && ch === '#') break;
|
|
768
|
+
if (mode !== 'py' && ch === '/' && next === '/') break;
|
|
769
|
+
if (mode !== 'py' && ch === '/' && next === '*') { inBlockComment = true; k += 2; continue; }
|
|
770
|
+
if (mode === 'py' && (next3 === '"""' || next3 === "'''")) { pyTripleQuote = next3; k += 3; continue; }
|
|
771
|
+
if (ch === '"' || ch === "'" || (mode === 'ts' && ch === '\x60') || (mode === 'go' && ch === '\x60')) {
|
|
772
|
+
const quote = ch;
|
|
773
|
+
k++;
|
|
774
|
+
while (k < len) {
|
|
775
|
+
if (quote !== '\x60' && raw[k] === '\\') { k += 2; continue; }
|
|
776
|
+
if (raw[k] === quote) { k++; break; }
|
|
777
|
+
k++;
|
|
778
|
+
}
|
|
779
|
+
continue;
|
|
780
|
+
}
|
|
781
|
+
if (ch === '(') parenDepth++;
|
|
782
|
+
else if (ch === ')') parenDepth = Math.max(0, parenDepth - 1);
|
|
783
|
+
else if (ch === '[') bracketDepth++;
|
|
784
|
+
else if (ch === ']') bracketDepth = Math.max(0, bracketDepth - 1);
|
|
785
|
+
else if (ch === '{') braceDepth++;
|
|
786
|
+
else if (ch === '}') braceDepth = Math.max(0, braceDepth - 1);
|
|
787
|
+
k++;
|
|
788
|
+
}
|
|
789
|
+
if (!inBlockComment && !pyTripleQuote && parenDepth <= 0 && bracketDepth <= 0 && braceDepth <= 0) {
|
|
790
|
+
const trimmedEnd = raw.trimEnd();
|
|
791
|
+
if (trimmedEnd.endsWith('\\')) continue;
|
|
792
|
+
if (trimmedEnd.endsWith('=') && !trimmedEnd.endsWith('=>')) continue;
|
|
793
|
+
return j;
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
return maxScan;
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
function extractTopLevelConstants(code, ext, existingBlocks) {
|
|
800
|
+
const e = (ext || '').toLowerCase();
|
|
801
|
+
const lines = String(code || '').replace(/\r\n/g, '\n').split('\n');
|
|
802
|
+
const covered = (existingBlocks || []).map((b) => [b.startLine, b.endLine]);
|
|
803
|
+
const isCovered = (ln) => covered.some(([s, en]) => ln >= s && ln <= en);
|
|
804
|
+
const out = [];
|
|
805
|
+
|
|
806
|
+
if (e === '.py' || e === '.pyw') {
|
|
807
|
+
for (let i = 0; i < lines.length; i++) {
|
|
808
|
+
const ln = i + 1;
|
|
809
|
+
if (isCovered(ln)) continue;
|
|
810
|
+
const line = lines[i];
|
|
811
|
+
const m = line.match(/^([A-Z][A-Z0-9_]+|__all__|__version__)\s*(?::\s*[^=\n]+)?=\s*(?!=)/);
|
|
812
|
+
if (!m) continue;
|
|
813
|
+
const endIdx = findStatementEndLine(lines, i, 'py');
|
|
814
|
+
const endLine = endIdx + 1;
|
|
815
|
+
covered.push([ln, endLine]);
|
|
816
|
+
const full = lines.slice(i, endLine).join('\n');
|
|
817
|
+
const lineCount = endLine - ln + 1;
|
|
818
|
+
const skeleton = lineCount <= 8 ? full : line + '\n # ... ' + (lineCount - 1) + ' lines folded';
|
|
819
|
+
out.push({ name: m[1], kind: 'const', exported: !m[1].startsWith('_') || m[1] === '__all__', startLine: ln, endLine, full, skeleton });
|
|
820
|
+
i = endIdx;
|
|
821
|
+
}
|
|
822
|
+
return out;
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
if (e === '.go') {
|
|
826
|
+
for (let i = 0; i < lines.length; i++) {
|
|
827
|
+
const ln = i + 1;
|
|
828
|
+
if (isCovered(ln)) continue;
|
|
829
|
+
const line = lines[i];
|
|
830
|
+
const gm = line.match(/^(const|var)\s*\(/);
|
|
831
|
+
if (gm) {
|
|
832
|
+
const endIdx = findStatementEndLine(lines, i, 'go');
|
|
833
|
+
const endLine = endIdx + 1;
|
|
834
|
+
covered.push([ln, endLine]);
|
|
835
|
+
const names = [];
|
|
836
|
+
for (let j = i + 1; j <= endIdx; j++) {
|
|
837
|
+
const im = lines[j].match(/^\s*([A-Za-z_][A-Za-z0-9_]*)\b/);
|
|
838
|
+
if (im && im[1] !== '_' && im[1] !== 'iota') names.push(im[1]);
|
|
839
|
+
}
|
|
840
|
+
const name = names.length > 0 ? names.slice(0, 4).join(', ') : gm[1] + '_block';
|
|
841
|
+
const full = lines.slice(i, endLine).join('\n');
|
|
842
|
+
const lineCount = endLine - ln + 1;
|
|
843
|
+
const skeleton = lineCount <= 12 ? full : line + '\n\t// ... ' + (lineCount - 2) + ' lines folded\n)';
|
|
844
|
+
out.push({ name, kind: 'const', exported: names.some((n) => /^[A-Z]/.test(n)), startLine: ln, endLine, full, skeleton });
|
|
845
|
+
i = endIdx;
|
|
846
|
+
continue;
|
|
847
|
+
}
|
|
848
|
+
const sm = line.match(/^(const|var)\s+([A-Za-z_][A-Za-z0-9_]*)\b/);
|
|
849
|
+
if (sm) {
|
|
850
|
+
const endIdx = findStatementEndLine(lines, i, 'go');
|
|
851
|
+
const endLine = endIdx + 1;
|
|
852
|
+
covered.push([ln, endLine]);
|
|
853
|
+
const full = lines.slice(i, endLine).join('\n');
|
|
854
|
+
const lineCount = endLine - ln + 1;
|
|
855
|
+
const skeleton = lineCount <= 8 ? full : line + '\n\t// ... ' + (lineCount - 1) + ' lines folded';
|
|
856
|
+
out.push({ name: sm[2], kind: 'const', exported: /^[A-Z]/.test(sm[2]), startLine: ln, endLine, full, skeleton });
|
|
857
|
+
i = endIdx;
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
return out;
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
if (e === '.rs') {
|
|
864
|
+
for (let i = 0; i < lines.length; i++) {
|
|
865
|
+
const ln = i + 1;
|
|
866
|
+
if (isCovered(ln)) continue;
|
|
867
|
+
const line = lines[i];
|
|
868
|
+
const rm = line.match(/^(?:pub(?:\([^)]+\))?\s+)?(?:const|static)\s+([A-Z][A-Z0-9_]*)\b/);
|
|
869
|
+
if (!rm) continue;
|
|
870
|
+
const endIdx = findStatementEndLine(lines, i, 'rs');
|
|
871
|
+
const endLine = endIdx + 1;
|
|
872
|
+
covered.push([ln, endLine]);
|
|
873
|
+
const full = lines.slice(i, endLine).join('\n');
|
|
874
|
+
const lineCount = endLine - ln + 1;
|
|
875
|
+
const skeleton = lineCount <= 8 ? full : line + '\n /* ... ' + (lineCount - 1) + ' lines folded */';
|
|
876
|
+
out.push({ name: rm[1], kind: 'const', exported: line.startsWith('pub'), startLine: ln, endLine, full, skeleton });
|
|
877
|
+
i = endIdx;
|
|
878
|
+
}
|
|
879
|
+
return out;
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
if (['.ts', '.tsx', '.mts', '.cts', '.js', '.jsx', '.mjs', '.cjs'].includes(e)) {
|
|
883
|
+
for (let i = 0; i < lines.length; i++) {
|
|
884
|
+
const ln = i + 1;
|
|
885
|
+
if (isCovered(ln)) continue;
|
|
886
|
+
const line = lines[i];
|
|
887
|
+
let name = null;
|
|
888
|
+
let exported = false;
|
|
889
|
+
const cm = line.match(/^(export\s+)?(?:declare\s+)?(?:const|let|var)\s+([A-Za-z_$][A-Za-z0-9_$]*)\b/);
|
|
890
|
+
if (cm) {
|
|
891
|
+
exported = Boolean(cm[1]);
|
|
892
|
+
if (exported || /^[A-Z][A-Z0-9_]+$/.test(cm[2])) name = cm[2];
|
|
893
|
+
} else {
|
|
894
|
+
const tm = line.match(/^(?:export\s+(default)\s+(?!(?:async\s+)?(?:function|class|abstract\s+class|interface)\b)|(module\.exports)\s*=)/);
|
|
895
|
+
if (tm) { name = tm[1] ? 'default' : 'module.exports'; exported = true; }
|
|
896
|
+
}
|
|
897
|
+
if (!name) continue;
|
|
898
|
+
const endIdx = findStatementEndLine(lines, i, 'ts');
|
|
899
|
+
const endLine = endIdx + 1;
|
|
900
|
+
const full = lines.slice(i, endLine).join('\n');
|
|
901
|
+
if (full.includes('=>') && !name.startsWith('default')) continue;
|
|
902
|
+
covered.push([ln, endLine]);
|
|
903
|
+
const lineCount = endLine - ln + 1;
|
|
904
|
+
const skeleton = lineCount <= 10 ? full : line + '\n /* ... ' + (lineCount - 1) + ' lines folded */';
|
|
905
|
+
out.push({ name, kind: 'const', exported, startLine: ln, endLine, full, skeleton });
|
|
906
|
+
i = endIdx;
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
return out;
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
function extractFileFooter(content, tailLines = 20) {
|
|
913
|
+
if (!content) return null;
|
|
914
|
+
const lines = String(content).replace(/\r\n/g, '\n').split('\n');
|
|
915
|
+
let lastNonEmpty = lines.length - 1;
|
|
916
|
+
while (lastNonEmpty >= 0 && lines[lastNonEmpty].trim() === '') lastNonEmpty--;
|
|
917
|
+
if (lastNonEmpty < 0) return null;
|
|
918
|
+
const windowStart = Math.max(0, lastNonEmpty - tailLines + 1);
|
|
919
|
+
const stripFooterNoise = (line) =>
|
|
920
|
+
line.replace(/(['"\x60])(?:\\.|(?!\1).)*\1/g, '""').replace(/\/\/.*$|#.*$/g, '');
|
|
921
|
+
const findTailStmtEnd = (startIdx) => {
|
|
922
|
+
let paren = 0, bracket = 0, brace = 0;
|
|
923
|
+
for (let j = startIdx; j <= lastNonEmpty; j++) {
|
|
924
|
+
const clean = stripFooterNoise(lines[j]);
|
|
925
|
+
for (let k = 0; k < clean.length; k++) {
|
|
926
|
+
const ch = clean[k];
|
|
927
|
+
if (ch === '(') paren++;
|
|
928
|
+
else if (ch === ')') paren = Math.max(0, paren - 1);
|
|
929
|
+
else if (ch === '[') bracket++;
|
|
930
|
+
else if (ch === ']') bracket = Math.max(0, bracket - 1);
|
|
931
|
+
else if (ch === '{') brace++;
|
|
932
|
+
else if (ch === '}') brace = Math.max(0, brace - 1);
|
|
933
|
+
}
|
|
934
|
+
if (paren <= 0 && bracket <= 0 && brace <= 0) {
|
|
935
|
+
const t = lines[j].trimEnd();
|
|
936
|
+
if (t.endsWith('\\') || (t.endsWith('=') && !t.endsWith('=>'))) continue;
|
|
937
|
+
return j;
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
return lastNonEmpty;
|
|
941
|
+
};
|
|
942
|
+
const footerStartRe =
|
|
943
|
+
/^(?:export\s+default\s+(?!(?:async\s+)?(?:function|class|abstract\s+class|interface)\b)|export\s+(?:type\s+)?\{|export\s+\*\s+from\b|export\s+(?:const|let|var)\s+|module\.exports\s*=|exports\.[A-Za-z_$][A-Za-z0-9_$]*\s*=|(?:pub(?:\([^)]+\))?\s+)?(?:const|static)\s+[A-Za-z_][A-Za-z0-9_]*|(?:const|var)\s*\(|(?:const|var)\s+[A-Z][A-Za-z0-9_]*\b|(?:[A-Z][A-Z0-9_]+|__all__|__version__)\s*(?::\s*[^=\n]+)?=\s*(?!=))/;
|
|
944
|
+
const chunks = [];
|
|
945
|
+
let i = windowStart;
|
|
946
|
+
while (i <= lastNonEmpty) {
|
|
947
|
+
const raw = lines[i];
|
|
948
|
+
if (!raw || /^\s/.test(raw)) { i++; continue; }
|
|
949
|
+
const trimmed = raw.trim();
|
|
950
|
+
if (trimmed.startsWith('//') || trimmed.startsWith('#') || trimmed.startsWith('/*') || !footerStartRe.test(raw)) {
|
|
951
|
+
i++; continue;
|
|
952
|
+
}
|
|
953
|
+
const endIdx = findTailStmtEnd(i);
|
|
954
|
+
chunks.push({ startLine: i + 1, endLine: endIdx + 1, text: lines.slice(i, endIdx + 1).join('\n') });
|
|
955
|
+
i = endIdx + 1;
|
|
956
|
+
}
|
|
957
|
+
if (chunks.length === 0) return null;
|
|
958
|
+
return {
|
|
959
|
+
footer: chunks.map((c) => c.text).join('\n'),
|
|
960
|
+
startLine: chunks[0].startLine,
|
|
961
|
+
endLine: chunks[chunks.length - 1].endLine,
|
|
962
|
+
chunks
|
|
963
|
+
};
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
|
|
731
967
|
const files = walk(targetRoot);
|
|
732
968
|
const terms = expandQueryTerms(query);
|
|
733
969
|
const allRawBlocks = [];
|
|
734
970
|
const fileHeaders = new Map();
|
|
971
|
+
const fileFooters = new Map();
|
|
972
|
+
const fileLineCounts = new Map();
|
|
735
973
|
for (const filePath of files) {
|
|
736
974
|
try {
|
|
737
975
|
const buf = fs.readFileSync(filePath);
|
|
738
976
|
if (isBinaryBuffer(buf)) continue;
|
|
739
977
|
const content = buf.toString('utf8');
|
|
740
978
|
const relPath = path.relative(targetRoot, filePath);
|
|
979
|
+
fileLineCounts.set(relPath, content.split('\n').length);
|
|
741
980
|
const hdr = extractFileHeader(content);
|
|
742
981
|
if (hdr) fileHeaders.set(relPath, hdr);
|
|
982
|
+
const ftr = extractFileFooter(content);
|
|
983
|
+
if (ftr) fileFooters.set(relPath, ftr);
|
|
743
984
|
const items = extractBlocks(content, path.extname(filePath), relPath);
|
|
744
985
|
for (const b of items) {
|
|
745
986
|
allRawBlocks.push({
|
|
@@ -781,13 +1022,20 @@ const scored = allRawBlocks.map((b) => {
|
|
|
781
1022
|
return { ...b, score };
|
|
782
1023
|
}).sort((a, b) => b.score - a.score);
|
|
783
1024
|
|
|
784
|
-
// True Call Graph (SymbolMap + Word-Boundary Call Matching + BFS maxHops)
|
|
1025
|
+
// True Call Graph (SymbolMap + Word-Boundary Call/Constant Matching + BFS maxHops)
|
|
785
1026
|
const symbolMap = new Map();
|
|
1027
|
+
const constSymbolNames = new Set();
|
|
786
1028
|
for (let i = 0; i < scored.length; i++) {
|
|
787
1029
|
const b = scored[i];
|
|
788
1030
|
if (b.name && b.kind !== 'module' && !CONTROL_WORDS.has(b.name)) {
|
|
789
|
-
|
|
790
|
-
|
|
1031
|
+
const names = (b.kind === 'const' && b.name.includes(','))
|
|
1032
|
+
? b.name.split(',').map((s) => s.trim()).filter(Boolean)
|
|
1033
|
+
: [b.name];
|
|
1034
|
+
for (const sn of names) {
|
|
1035
|
+
if (!symbolMap.has(sn)) symbolMap.set(sn, []);
|
|
1036
|
+
symbolMap.get(sn).push(i);
|
|
1037
|
+
if (b.kind === 'const') constSymbolNames.add(sn);
|
|
1038
|
+
}
|
|
791
1039
|
}
|
|
792
1040
|
}
|
|
793
1041
|
const callAdj = Array.from({ length: scored.length }, () => new Set());
|
|
@@ -799,7 +1047,9 @@ for (let i = 0; i < scored.length; i++) {
|
|
|
799
1047
|
if (sym === caller.name) continue;
|
|
800
1048
|
if (maskedCaller.includes(sym)) {
|
|
801
1049
|
const esc = sym.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
802
|
-
|
|
1050
|
+
const isConstRef = constSymbolNames.has(sym) && sym.length >= 3;
|
|
1051
|
+
const re = isConstRef ? new RegExp('\\b' + esc + '\\b', 'm') : new RegExp('\\b' + esc + '\\s*\\(', 'm');
|
|
1052
|
+
if (re.test(maskedCaller)) {
|
|
803
1053
|
for (const t of (symbolMap.get(sym) || [])) {
|
|
804
1054
|
callAdj[i].add(t);
|
|
805
1055
|
callAdj[t].add(i);
|
|
@@ -809,11 +1059,13 @@ for (let i = 0; i < scored.length; i++) {
|
|
|
809
1059
|
}
|
|
810
1060
|
}
|
|
811
1061
|
const hopDistances = new Map();
|
|
1062
|
+
const focalFiles = new Set();
|
|
812
1063
|
const bfsQ = [];
|
|
813
1064
|
for (let i = 0; i < scored.length; i++) {
|
|
814
1065
|
if (scored[i].score > 0 && bfsQ.length < 3) {
|
|
815
1066
|
bfsQ.push(i);
|
|
816
1067
|
hopDistances.set(i, 0);
|
|
1068
|
+
focalFiles.add(scored[i].filePath);
|
|
817
1069
|
}
|
|
818
1070
|
}
|
|
819
1071
|
let qHead = 0;
|
|
@@ -830,10 +1082,16 @@ while (qHead < bfsQ.length) {
|
|
|
830
1082
|
}
|
|
831
1083
|
|
|
832
1084
|
let remaining = budget;
|
|
833
|
-
const out = ['<context_slice root="' + path.basename(targetRoot) + '" version="0.3.
|
|
1085
|
+
const out = ['<context_slice root="' + path.basename(targetRoot) + '" version="0.3.5" budget="' + budget + '">'];
|
|
834
1086
|
const includedIds = new Set();
|
|
835
1087
|
const includedFiles = new Set();
|
|
836
1088
|
const fullStart1EndLine = new Map();
|
|
1089
|
+
const coveredRangesByFile = new Map();
|
|
1090
|
+
const partialFiles = new Set();
|
|
1091
|
+
const addCoveredRange = (fp, s, e) => {
|
|
1092
|
+
if (!coveredRangesByFile.has(fp)) coveredRangesByFile.set(fp, []);
|
|
1093
|
+
coveredRangesByFile.get(fp).push([s, e]);
|
|
1094
|
+
};
|
|
837
1095
|
const dirBudgetUsed = {};
|
|
838
1096
|
const maxDirBudget = budget * 0.65;
|
|
839
1097
|
|
|
@@ -851,34 +1109,44 @@ for (let i = 0; i < scored.length; i++) {
|
|
|
851
1109
|
dirBudgetUsed[topDir] = curDir + item.rawTokens;
|
|
852
1110
|
includedIds.add(key);
|
|
853
1111
|
includedFiles.add(item.filePath);
|
|
1112
|
+
addCoveredRange(item.filePath, item.startLine, item.endLine);
|
|
854
1113
|
if (item.startLine === 1) {
|
|
855
1114
|
fullStart1EndLine.set(item.filePath, Math.max(fullStart1EndLine.get(item.filePath) || 0, item.endLine));
|
|
856
1115
|
}
|
|
857
1116
|
out.push(` <!-- FULL: ${item.filePath}:${item.startLine}-${item.endLine} (${item.rawTokens} tokens) -->`);
|
|
858
1117
|
out.push(item.full);
|
|
859
1118
|
} else if (item.skelTokens <= remaining && curDir + item.skelTokens <= maxDirBudget) {
|
|
860
|
-
const
|
|
1119
|
+
const sameFileConst = item.kind === 'const' && (item.exported || /^[A-Z][A-Z0-9_]+$/.test(item.name)) && (item.endLine - item.startLine + 1) <= 25 && focalFiles.has(item.filePath);
|
|
1120
|
+
const linked = (hop !== undefined && hop <= 2) || item.score > 0 || sameFileConst;
|
|
861
1121
|
if (linked || i < 12) {
|
|
862
1122
|
remaining -= item.skelTokens;
|
|
863
1123
|
dirBudgetUsed[topDir] = curDir + item.skelTokens;
|
|
864
1124
|
includedIds.add(key);
|
|
865
1125
|
includedFiles.add(item.filePath);
|
|
1126
|
+
partialFiles.add(item.filePath);
|
|
1127
|
+
if (item.skeleton === item.full) {
|
|
1128
|
+
addCoveredRange(item.filePath, item.startLine, item.endLine);
|
|
1129
|
+
}
|
|
866
1130
|
out.push(` <!-- SKELETON: ${item.filePath}:${item.startLine}-${item.endLine} (${item.skelTokens} tokens) -->`);
|
|
867
1131
|
out.push(item.skeleton);
|
|
868
1132
|
} else {
|
|
1133
|
+
partialFiles.add(item.filePath);
|
|
869
1134
|
candidateItems.push(item);
|
|
870
1135
|
}
|
|
871
1136
|
} else {
|
|
1137
|
+
partialFiles.add(item.filePath);
|
|
872
1138
|
candidateItems.push(item);
|
|
873
1139
|
}
|
|
874
1140
|
}
|
|
875
1141
|
|
|
876
1142
|
const headerChunks = [];
|
|
1143
|
+
const headerEndByFile = new Map();
|
|
877
1144
|
for (const fp of includedFiles) {
|
|
878
1145
|
const hdr = fileHeaders.get(fp);
|
|
879
1146
|
if (!hdr) continue;
|
|
880
1147
|
const coveredEnd = fullStart1EndLine.get(fp) || 0;
|
|
881
1148
|
if (coveredEnd >= hdr.endLine) continue;
|
|
1149
|
+
headerEndByFile.set(fp, hdr.endLine);
|
|
882
1150
|
headerChunks.push(` <!-- FILE_HEADER: ${fp}:1-${hdr.endLine} -->`);
|
|
883
1151
|
headerChunks.push(hdr.header);
|
|
884
1152
|
}
|
|
@@ -886,6 +1154,25 @@ if (headerChunks.length > 0) {
|
|
|
886
1154
|
out.splice(1, 0, '<file_headers>', ...headerChunks, '</file_headers>');
|
|
887
1155
|
}
|
|
888
1156
|
|
|
1157
|
+
const footerChunks = [];
|
|
1158
|
+
for (const fp of includedFiles) {
|
|
1159
|
+
const ftr = fileFooters.get(fp);
|
|
1160
|
+
if (!ftr) continue;
|
|
1161
|
+
const ranges = coveredRangesByFile.get(fp) || [];
|
|
1162
|
+
const totalLines = fileLineCounts.get(fp) || 0;
|
|
1163
|
+
const wholeFileCovered = ranges.some(([s, e]) => s <= 1 && e >= totalLines);
|
|
1164
|
+
if (!partialFiles.has(fp) && wholeFileCovered) continue;
|
|
1165
|
+
const hdrEnd = headerEndByFile.get(fp) || 0;
|
|
1166
|
+
const uncovered = ftr.chunks.filter((c) => c.startLine > hdrEnd && !ranges.some(([s, e]) => c.startLine >= s && c.endLine <= e));
|
|
1167
|
+
if (uncovered.length > 0) {
|
|
1168
|
+
footerChunks.push(` <!-- FILE_FOOTER: ${fp}:${uncovered[0].startLine}-${uncovered[uncovered.length - 1].endLine} -->`);
|
|
1169
|
+
footerChunks.push(uncovered.map((c) => c.text).join('\n'));
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
if (footerChunks.length > 0) {
|
|
1173
|
+
out.push('<file_footers>', ...footerChunks, '</file_footers>');
|
|
1174
|
+
}
|
|
1175
|
+
|
|
889
1176
|
const omitted = candidateItems.filter(item => item.score > 0 && !includedIds.has(item.filePath + ':' + item.startLine)).slice(0, 3);
|
|
890
1177
|
if (omitted.length > 0) {
|
|
891
1178
|
out.push('<omitted_top_matches>');
|
package/package.json
CHANGED