yats-toolkit 0.4.2 → 0.5.1
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/setup.js +8 -6
- package/connect/claude/SKILL.md +36 -0
- package/connect/claude/mcp.json +8 -0
- package/connect/codex/AGENTS.md +37 -0
- package/connect/codex/config.toml +9 -0
- package/connect/copilot/instructions.md +18 -0
- package/connect/copilot/mcp.json +10 -0
- package/connect/cursor/mcp.json +7 -0
- package/connect/cursor/rules.mdc +27 -0
- package/connect/gemini/GEMINI.md +33 -0
- package/connect/gemini/mcp.json +9 -0
- package/package.json +4 -2
- package/src/benchmark.js +23 -136
- package/src/connect.js +219 -52
- package/src/indexer.js +300 -16
- package/src/indexer.test.js +274 -0
- package/src/setup.js +3 -3
- package/src/watch.js +368 -109
- package/src/watch.test.js +197 -0
package/src/indexer.js
CHANGED
|
@@ -1,25 +1,242 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* yats index <path> — Index a repository by
|
|
3
|
-
* Reads each file from the host and POSTs it
|
|
2
|
+
* yats index <path> [--skip-docs] [--no-config] — Index a repository by
|
|
3
|
+
* sending files to YATS server. Reads each file from the host and POSTs it
|
|
4
|
+
* via HTTP.
|
|
5
|
+
*
|
|
6
|
+
* --skip-docs: skip documentation files (DOC_EXTENSIONS, default .md/.mdx/
|
|
7
|
+
* .rst/.txt/.adoc/.org/.wiki/.readme) instead of only .md files.
|
|
8
|
+
* --no-config: ignore the repo's .yats/config.json and use machine defaults.
|
|
9
|
+
*
|
|
10
|
+
* Per-repo config (`.yats/config.json`, all keys optional):
|
|
11
|
+
*
|
|
12
|
+
* {
|
|
13
|
+
* "indexDocs": false, // skip ALL docs for this repo
|
|
14
|
+
* "docExtensions": [".md"], // REPLACES the machine's DOC_EXTENSIONS
|
|
15
|
+
* "docPatterns": ["docs/", "README.md"], // whitelist: only these doc files are sent
|
|
16
|
+
* "ignoredDirs": ["sandbox"], // ADDED to the machine's IGNORED_DIRS
|
|
17
|
+
* "skipExtensions": [".snap"] // ADDED to the machine's SKIP_EXTENSIONS
|
|
18
|
+
* }
|
|
4
19
|
*/
|
|
5
|
-
import { readFileSync, statSync, readdirSync } from "node:fs";
|
|
6
|
-
import { join, relative
|
|
20
|
+
import { readFileSync, statSync, readdirSync, readSync } from "node:fs";
|
|
21
|
+
import { join, relative } from "node:path";
|
|
22
|
+
import { homedir } from "node:os";
|
|
23
|
+
import { spawnSync } from "node:child_process";
|
|
7
24
|
|
|
8
25
|
const YATS_URL = process.env.YATS_URL || "http://localhost:5555";
|
|
9
26
|
|
|
10
|
-
const
|
|
27
|
+
const CONFIG_SCHEMA_HINT = `{
|
|
28
|
+
"indexDocs": false,
|
|
29
|
+
"docExtensions": [".md"],
|
|
30
|
+
"docPatterns": ["docs/", "README.md"],
|
|
31
|
+
"ignoredDirs": ["sandbox"],
|
|
32
|
+
"skipExtensions": [".snap"]
|
|
33
|
+
}`;
|
|
34
|
+
|
|
35
|
+
export const IGNORED = new Set([
|
|
11
36
|
"node_modules", ".git", "dist", "build", ".next", "__pycache__",
|
|
12
37
|
"vendor", "target", "bin", "obj", ".venv", "venv", ".yarn", ".pnpm",
|
|
13
38
|
]);
|
|
14
39
|
|
|
15
|
-
|
|
40
|
+
export const DEFAULT_DOC_EXTENSIONS = ".md,.mdx,.rst,.txt,.adoc,.org,.wiki,.readme";
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Read the YATS config written by `yats setup` (~/.yats/.env), so the CLI
|
|
44
|
+
* filters files consistently with the server (DOC_EXTENSIONS, SKIP_EXTENSIONS,
|
|
45
|
+
* IGNORED_DIRS). Accepts an explicit path for tests.
|
|
46
|
+
*/
|
|
47
|
+
export function loadYatsEnv(envPath = null) {
|
|
48
|
+
const file = envPath ?? join(homedir(), ".yats", ".env");
|
|
49
|
+
const env = {};
|
|
50
|
+
try {
|
|
51
|
+
for (const line of readFileSync(file, "utf-8").split("\n")) {
|
|
52
|
+
if (!line.trim() || line.trim().startsWith("#")) continue;
|
|
53
|
+
const m = line.match(/^\s*([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)\s*$/);
|
|
54
|
+
if (m) env[m[1]] = m[2];
|
|
55
|
+
}
|
|
56
|
+
} catch {
|
|
57
|
+
// No .env — use built-in defaults.
|
|
58
|
+
}
|
|
59
|
+
return env;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Decide whether a repository-relative path must be skipped client-side.
|
|
64
|
+
* Pure helper (exported for tests):
|
|
65
|
+
* - skipDocs: drop files whose extension is in docExtensions
|
|
66
|
+
* - skipExtensions: drop files ending with any of these suffixes
|
|
67
|
+
* - docPatterns (prefix whitelist): when set, doc files are only sent if
|
|
68
|
+
* their path starts with one of the prefixes (code files always sent)
|
|
69
|
+
*/
|
|
70
|
+
export function shouldSkipFile(
|
|
71
|
+
relPath,
|
|
72
|
+
{ skipDocs = false, docExtensions = [], skipExtensions = [], docPatterns = null } = {},
|
|
73
|
+
) {
|
|
74
|
+
const lower = relPath.toLowerCase();
|
|
75
|
+
const isDoc = docExtensions.some((e) => lower.endsWith(e));
|
|
76
|
+
if (isDoc) {
|
|
77
|
+
if (skipDocs) return true;
|
|
78
|
+
if (docPatterns && !docPatterns.some((p) => relPath.startsWith(p))) return true;
|
|
79
|
+
}
|
|
80
|
+
if (skipExtensions.some((e) => lower.endsWith(e))) return true;
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// ============================================================
|
|
85
|
+
// Per-repo config — .yats/config.json (P6)
|
|
86
|
+
// ============================================================
|
|
87
|
+
|
|
88
|
+
/** Validate a parsed repo config. Returns {ok:false,error} with a human
|
|
89
|
+
* (and AI-agent) readable explanation of what is wrong. */
|
|
90
|
+
export function validateRepoConfig(raw) {
|
|
91
|
+
let data;
|
|
92
|
+
try {
|
|
93
|
+
data = JSON.parse(raw);
|
|
94
|
+
} catch (err) {
|
|
95
|
+
return { ok: false, error: `not valid JSON: ${err.message}` };
|
|
96
|
+
}
|
|
97
|
+
if (typeof data !== "object" || data === null || Array.isArray(data)) {
|
|
98
|
+
return { ok: false, error: "must be a JSON object (not an array or a plain value)" };
|
|
99
|
+
}
|
|
100
|
+
const errors = [];
|
|
101
|
+
if (data.indexDocs !== undefined && typeof data.indexDocs !== "boolean") {
|
|
102
|
+
errors.push('"indexDocs" must be true or false');
|
|
103
|
+
}
|
|
104
|
+
for (const key of ["docExtensions", "docPatterns", "ignoredDirs", "skipExtensions"]) {
|
|
105
|
+
if (data[key] !== undefined && (!Array.isArray(data[key]) || !data[key].every((v) => typeof v === "string"))) {
|
|
106
|
+
errors.push(`"${key}" must be an array of strings`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
if (errors.length > 0) return { ok: false, error: errors.join("; ") };
|
|
110
|
+
return { ok: true, config: data };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Load and validate the repo's .yats/config.json.
|
|
115
|
+
* Returns { ok:true, config, path, exists } or { ok:false, error, path, exists }.
|
|
116
|
+
* A missing file is NOT an error — machine defaults apply.
|
|
117
|
+
*/
|
|
118
|
+
export function loadRepoConfig(repoPath) {
|
|
119
|
+
const configPath = join(repoPath, ".yats", "config.json");
|
|
120
|
+
let raw;
|
|
121
|
+
try {
|
|
122
|
+
raw = readFileSync(configPath, "utf-8");
|
|
123
|
+
} catch {
|
|
124
|
+
return { ok: true, config: {}, path: configPath, exists: false };
|
|
125
|
+
}
|
|
126
|
+
const result = validateRepoConfig(raw);
|
|
127
|
+
return { ...result, path: configPath, exists: true };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Merge the machine config (~/.yats/.env) with the repo config.
|
|
132
|
+
* Rule: a repo can only NARROW what the machine allows — ignoredDirs and
|
|
133
|
+
* skipExtensions are UNIONs, docExtensions REPLACES the global list, and
|
|
134
|
+
* indexDocs=false can only turn docs off (never on, if the machine forbids).
|
|
135
|
+
*/
|
|
136
|
+
export function mergeRepoConfig(globalEnv, repoConfig) {
|
|
137
|
+
const toList = (v) => v.split(",").map((e) => e.trim().toLowerCase()).filter(Boolean);
|
|
138
|
+
|
|
139
|
+
const docExtensions = Array.isArray(repoConfig.docExtensions)
|
|
140
|
+
? repoConfig.docExtensions.map((e) => e.trim().toLowerCase()).filter(Boolean)
|
|
141
|
+
: toList(globalEnv.DOC_EXTENSIONS ?? DEFAULT_DOC_EXTENSIONS);
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
docExtensions,
|
|
145
|
+
skipExtensions: [
|
|
146
|
+
...toList(globalEnv.SKIP_EXTENSIONS ?? ""),
|
|
147
|
+
...(Array.isArray(repoConfig.skipExtensions) ? repoConfig.skipExtensions : []),
|
|
148
|
+
],
|
|
149
|
+
ignoredDirs: [
|
|
150
|
+
...toList(globalEnv.IGNORED_DIRS ?? ""),
|
|
151
|
+
...(Array.isArray(repoConfig.ignoredDirs) ? repoConfig.ignoredDirs : []),
|
|
152
|
+
],
|
|
153
|
+
docPatterns: Array.isArray(repoConfig.docPatterns) ? repoConfig.docPatterns : null,
|
|
154
|
+
indexDocs: repoConfig.indexDocs !== false,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* The message printed when .yats/config.json is invalid. Written for BOTH
|
|
160
|
+
* humans and AI agents running `yats index`: it explains what is wrong, why
|
|
161
|
+
* indexing stopped (a broken filter could index something huge), and the
|
|
162
|
+
* three ways out (fix + re-run, delete the file, or --no-config).
|
|
163
|
+
*/
|
|
164
|
+
export function configErrorText(repoPath, configPath, error) {
|
|
165
|
+
return [
|
|
166
|
+
`✗ Invalid repo config: ${configPath}`,
|
|
167
|
+
``,
|
|
168
|
+
` ${error}`,
|
|
169
|
+
``,
|
|
170
|
+
` Indexing stopped before reading any file. A broken config could mean`,
|
|
171
|
+
` wrong filters (e.g. indexing a huge folder that was meant to be ignored).`,
|
|
172
|
+
``,
|
|
173
|
+
` How to fix — pick ONE:`,
|
|
174
|
+
``,
|
|
175
|
+
` 1. Correct ${configPath} (valid JSON, schema below) and re-run:`,
|
|
176
|
+
``,
|
|
177
|
+
` yats index ${repoPath}`,
|
|
178
|
+
``,
|
|
179
|
+
` 2. Delete the file to fall back to the machine defaults (~/.yats/.env).`,
|
|
180
|
+
``,
|
|
181
|
+
` 3. Re-run with --no-config to ignore the file for this run only:`,
|
|
182
|
+
``,
|
|
183
|
+
` yats index ${repoPath} --no-config`,
|
|
184
|
+
``,
|
|
185
|
+
` Valid schema (every key is optional):`,
|
|
186
|
+
``,
|
|
187
|
+
...CONFIG_SCHEMA_HINT.split("\n").map((l) => ` ${l}`),
|
|
188
|
+
``,
|
|
189
|
+
` Semantics: ignoredDirs/skipExtensions ADD to the machine config;`,
|
|
190
|
+
` docExtensions REPLACES it; docPatterns whitelists which doc files are`,
|
|
191
|
+
` sent (prefix match); indexDocs=false skips all docs for this repo.`,
|
|
192
|
+
].join("\n");
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function promptYesNo(question) {
|
|
196
|
+
process.stdout.write(question);
|
|
197
|
+
try {
|
|
198
|
+
const buf = Buffer.alloc(16);
|
|
199
|
+
const n = readSync(0, buf, 0, 16);
|
|
200
|
+
return buf.toString("utf-8", 0, n).trim().toLowerCase() === "y";
|
|
201
|
+
} catch {
|
|
202
|
+
return false;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Resolve the repo config, stopping the run on a malformed file.
|
|
208
|
+
* - Non-TTY (the normal case — an AI agent runs yats): exit 1 with the
|
|
209
|
+
* configErrorText so the agent can fix the file and re-run.
|
|
210
|
+
* - TTY (a human): ask whether to open $EDITOR to fix it, re-validate and
|
|
211
|
+
* retry in a loop until valid or declined.
|
|
212
|
+
*/
|
|
213
|
+
export function resolveRepoConfig(repoPath, noConfig = false) {
|
|
214
|
+
if (noConfig) return {};
|
|
215
|
+
for (;;) {
|
|
216
|
+
const res = loadRepoConfig(repoPath);
|
|
217
|
+
if (res.ok) return res.config;
|
|
218
|
+
|
|
219
|
+
console.error(configErrorText(repoPath, res.path, res.error));
|
|
220
|
+
|
|
221
|
+
if (!process.stdin.isTTY) process.exit(1);
|
|
222
|
+
|
|
223
|
+
const editor = process.env.EDITOR || process.env.VISUAL || "vi";
|
|
224
|
+
const answer = promptYesNo(`\n Open ${editor} to fix it and retry? [y/N] `);
|
|
225
|
+
if (!answer) process.exit(1);
|
|
226
|
+
spawnSync(editor, [res.path], { stdio: "inherit" });
|
|
227
|
+
console.log(" Re-reading the config...");
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
async function walk(dir, ignored) {
|
|
16
233
|
const files = [];
|
|
17
234
|
const entries = readdirSync(dir, { withFileTypes: true });
|
|
18
235
|
for (const e of entries) {
|
|
19
|
-
if (
|
|
236
|
+
if (ignored.has(e.name) || e.name.startsWith(".")) continue;
|
|
20
237
|
const full = join(dir, e.name);
|
|
21
238
|
if (e.isDirectory()) {
|
|
22
|
-
files.push(...await walk(full));
|
|
239
|
+
files.push(...await walk(full, ignored));
|
|
23
240
|
} else if (e.isFile()) {
|
|
24
241
|
files.push(full);
|
|
25
242
|
}
|
|
@@ -28,20 +245,37 @@ async function walk(dir) {
|
|
|
28
245
|
}
|
|
29
246
|
|
|
30
247
|
export default async function indexRepo(args, options = {}) {
|
|
31
|
-
const
|
|
248
|
+
const skipDocsFlag = options.skipDocs || false;
|
|
249
|
+
const noConfig = options.noConfig || false;
|
|
32
250
|
const repoPath = args[0];
|
|
33
251
|
if (!repoPath) {
|
|
34
|
-
console.error("Usage: npx yats index <path>");
|
|
252
|
+
console.error("Usage: npx yats index <path> [--skip-docs] [--no-config]");
|
|
35
253
|
process.exit(1);
|
|
36
254
|
}
|
|
37
255
|
|
|
38
|
-
|
|
39
|
-
|
|
256
|
+
// The repository identity is its full path (two clones of the same repo are
|
|
257
|
+
// two distinct indexes) — never the basename.
|
|
258
|
+
const repoName = repoPath;
|
|
259
|
+
let stat;
|
|
260
|
+
try {
|
|
261
|
+
stat = statSync(repoPath);
|
|
262
|
+
} catch {
|
|
263
|
+
console.error(`Path not found: ${repoPath}`);
|
|
264
|
+
process.exit(1);
|
|
265
|
+
}
|
|
40
266
|
if (!stat.isDirectory()) {
|
|
41
267
|
console.error(`Not a directory: ${repoPath}`);
|
|
42
268
|
process.exit(1);
|
|
43
269
|
}
|
|
44
270
|
|
|
271
|
+
// P4/P6 — machine config (~/.yats/.env) + repo config (.yats/config.json).
|
|
272
|
+
// A malformed repo config STOPS the run (never index with broken filters).
|
|
273
|
+
const yatsEnv = loadYatsEnv();
|
|
274
|
+
const repoConfig = resolveRepoConfig(repoPath, noConfig);
|
|
275
|
+
const effective = mergeRepoConfig(yatsEnv, repoConfig);
|
|
276
|
+
const skipDocs = skipDocsFlag || effective.indexDocs === false;
|
|
277
|
+
const ignoredDirs = new Set([...IGNORED, ...effective.ignoredDirs]);
|
|
278
|
+
|
|
45
279
|
// Register repo
|
|
46
280
|
try {
|
|
47
281
|
await fetch(`${YATS_URL}/index`, {
|
|
@@ -56,14 +290,19 @@ export default async function indexRepo(args, options = {}) {
|
|
|
56
290
|
|
|
57
291
|
// Walk and send files
|
|
58
292
|
console.log(`Indexing ${repoPath}...`);
|
|
59
|
-
const files = await walk(repoPath);
|
|
293
|
+
const files = await walk(repoPath, ignoredDirs);
|
|
60
294
|
// Send files concurrently in batches
|
|
61
295
|
const CONCURRENCY = 10;
|
|
62
296
|
const batch = [];
|
|
63
|
-
|
|
297
|
+
|
|
64
298
|
for (const file of files) {
|
|
65
299
|
const relPath = relative(repoPath, file);
|
|
66
|
-
if (
|
|
300
|
+
if (shouldSkipFile(relPath, {
|
|
301
|
+
skipDocs,
|
|
302
|
+
docExtensions: effective.docExtensions,
|
|
303
|
+
skipExtensions: effective.skipExtensions,
|
|
304
|
+
docPatterns: effective.docPatterns,
|
|
305
|
+
})) continue;
|
|
67
306
|
batch.push(file);
|
|
68
307
|
}
|
|
69
308
|
|
|
@@ -108,10 +347,55 @@ export default async function indexRepo(args, options = {}) {
|
|
|
108
347
|
});
|
|
109
348
|
const data = await res.json();
|
|
110
349
|
if (res.ok) {
|
|
111
|
-
console.log(` ✓ Graph finalized
|
|
350
|
+
console.log(` ✓ Graph finalized (${data.stored} relationships in final flush)`);
|
|
112
351
|
}
|
|
113
352
|
} catch {
|
|
114
353
|
// Non-fatal — the server flushes on its own debounce timer.
|
|
115
354
|
}
|
|
355
|
+
|
|
356
|
+
// Record the indexed commit so `yats watch` knows where to start diffing
|
|
357
|
+
// from (P1: the index reflects the last commit, not the last save).
|
|
358
|
+
try {
|
|
359
|
+
const { execSync } = await import("node:child_process");
|
|
360
|
+
const commit = execSync("git rev-parse HEAD", {
|
|
361
|
+
cwd: repoPath,
|
|
362
|
+
encoding: "utf-8",
|
|
363
|
+
stdio: "pipe",
|
|
364
|
+
}).trim();
|
|
365
|
+
if (commit) {
|
|
366
|
+
await fetch(`${YATS_URL}/index/commit`, {
|
|
367
|
+
method: "POST",
|
|
368
|
+
headers: { "Content-Type": "application/json" },
|
|
369
|
+
body: JSON.stringify({ repository: repoName, commit }),
|
|
370
|
+
});
|
|
371
|
+
console.log(` ✓ Indexed commit recorded: ${commit.slice(0, 8)}`);
|
|
372
|
+
}
|
|
373
|
+
} catch {
|
|
374
|
+
// Not a git repo (or server unreachable) — watch will handle it.
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// Report the real totals from the graph. The server flushes incrementally
|
|
378
|
+
// while files stream in, so the final flush above may report 0 even though
|
|
379
|
+
// thousands of relationships are already stored.
|
|
380
|
+
try {
|
|
381
|
+
const res = await fetch(`${YATS_URL}/mcp`, {
|
|
382
|
+
method: "POST",
|
|
383
|
+
headers: { "Content-Type": "application/json" },
|
|
384
|
+
body: JSON.stringify({
|
|
385
|
+
jsonrpc: "2.0",
|
|
386
|
+
id: 1,
|
|
387
|
+
method: "tools/call",
|
|
388
|
+
params: { name: "repository_summary", arguments: { path: repoName } },
|
|
389
|
+
}),
|
|
390
|
+
});
|
|
391
|
+
const data = await res.json();
|
|
392
|
+
const text = data?.result?.content?.[0]?.text;
|
|
393
|
+
if (text) {
|
|
394
|
+
const summary = JSON.parse(text);
|
|
395
|
+
console.log(` ✓ Indexed ${summary.totalSymbols} symbols, ${summary.totalRelationships} relationships`);
|
|
396
|
+
}
|
|
397
|
+
} catch {
|
|
398
|
+
// Non-fatal — summary is informational.
|
|
399
|
+
}
|
|
116
400
|
console.log(``);
|
|
117
401
|
}
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests — P4: CLI-side config respect (indexer.js helpers).
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { describe, it, before, after } from "node:test";
|
|
6
|
+
import assert from "node:assert/strict";
|
|
7
|
+
import { writeFileSync, mkdirSync, rmSync } from "node:fs";
|
|
8
|
+
import { join } from "node:path";
|
|
9
|
+
import { tmpdir } from "node:os";
|
|
10
|
+
import { execFileSync } from "node:child_process";
|
|
11
|
+
import { fileURLToPath } from "node:url";
|
|
12
|
+
|
|
13
|
+
import {
|
|
14
|
+
loadYatsEnv,
|
|
15
|
+
shouldSkipFile,
|
|
16
|
+
loadRepoConfig,
|
|
17
|
+
validateRepoConfig,
|
|
18
|
+
mergeRepoConfig,
|
|
19
|
+
configErrorText,
|
|
20
|
+
DEFAULT_DOC_EXTENSIONS,
|
|
21
|
+
} from "./indexer.js";
|
|
22
|
+
|
|
23
|
+
const __dirname = fileURLToPath(new URL(".", import.meta.url));
|
|
24
|
+
const SETUP_JS = join(__dirname, "..", "bin", "setup.js");
|
|
25
|
+
|
|
26
|
+
describe("loadYatsEnv", () => {
|
|
27
|
+
let dir;
|
|
28
|
+
let envPath;
|
|
29
|
+
|
|
30
|
+
before(() => {
|
|
31
|
+
dir = join(tmpdir(), `yats-env-test-${Date.now()}`);
|
|
32
|
+
mkdirSync(dir, { recursive: true });
|
|
33
|
+
envPath = join(dir, ".env");
|
|
34
|
+
writeFileSync(
|
|
35
|
+
envPath,
|
|
36
|
+
"# comment line\n" +
|
|
37
|
+
"DOC_EXTENSIONS=.md,.rst\n" +
|
|
38
|
+
"IGNORED_DIRS=node_modules,dist,vendor\n" +
|
|
39
|
+
"SKIP_EXTENSIONS=.min.js,.lock\n" +
|
|
40
|
+
"EMPTY_VALUE=\n" +
|
|
41
|
+
"NO_EQUALS_SIGN\n",
|
|
42
|
+
);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
after(() => {
|
|
46
|
+
try { rmSync(dir, { recursive: true }); } catch {}
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("parses KEY=VALUE lines and ignores comments and malformed lines", () => {
|
|
50
|
+
const env = loadYatsEnv(envPath);
|
|
51
|
+
assert.equal(env.DOC_EXTENSIONS, ".md,.rst");
|
|
52
|
+
assert.equal(env.IGNORED_DIRS, "node_modules,dist,vendor");
|
|
53
|
+
assert.equal(env.SKIP_EXTENSIONS, ".min.js,.lock");
|
|
54
|
+
assert.equal(env.EMPTY_VALUE, "");
|
|
55
|
+
assert.equal(env.NO_EQUALS_SIGN, undefined);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("returns an empty object when the file is missing", () => {
|
|
59
|
+
const env = loadYatsEnv(join(dir, "does-not-exist.env"));
|
|
60
|
+
assert.deepEqual(env, {});
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
describe("shouldSkipFile", () => {
|
|
65
|
+
const docExtensions = DEFAULT_DOC_EXTENSIONS
|
|
66
|
+
.split(",").map((e) => e.trim().toLowerCase()).filter(Boolean);
|
|
67
|
+
|
|
68
|
+
it("skips only .md when skipDocs is false", () => {
|
|
69
|
+
assert.equal(shouldSkipFile("README.md", { skipDocs: false, docExtensions }), false);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("skips every DOC_EXTENSIONS file with --skip-docs, not only .md (P4)", () => {
|
|
73
|
+
for (const ext of docExtensions) {
|
|
74
|
+
assert.equal(
|
|
75
|
+
shouldSkipFile(`docs/guide${ext}`, { skipDocs: true, docExtensions }),
|
|
76
|
+
true,
|
|
77
|
+
`docs/guide${ext} should be skipped`,
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
assert.equal(
|
|
81
|
+
shouldSkipFile("src/code.py", { skipDocs: true, docExtensions }),
|
|
82
|
+
false,
|
|
83
|
+
"code files are never skipped by --skip-docs",
|
|
84
|
+
);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("skips SKIP_EXTENSIONS suffixes including compound ones like .min.js", () => {
|
|
88
|
+
const skipExtensions = [".min.js", ".lock"];
|
|
89
|
+
assert.equal(shouldSkipFile("app.min.js", { skipExtensions }), true);
|
|
90
|
+
assert.equal(shouldSkipFile("yarn.lock", { skipExtensions }), true);
|
|
91
|
+
assert.equal(shouldSkipFile("app.js", { skipExtensions }), false);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("is case-insensitive", () => {
|
|
95
|
+
assert.equal(
|
|
96
|
+
shouldSkipFile("README.MD", { skipDocs: true, docExtensions }),
|
|
97
|
+
true,
|
|
98
|
+
);
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
describe(".yats/config.json — repo config (P6)", () => {
|
|
103
|
+
let repo;
|
|
104
|
+
|
|
105
|
+
before(() => {
|
|
106
|
+
repo = join(tmpdir(), `yats-repo-config-test-${Date.now()}`);
|
|
107
|
+
mkdirSync(join(repo, ".yats"), { recursive: true });
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
after(() => {
|
|
111
|
+
try { rmSync(repo, { recursive: true }); } catch {}
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
function writeConfig(content) {
|
|
115
|
+
writeFileSync(join(repo, ".yats", "config.json"), content);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
it("returns defaults when the file does not exist", () => {
|
|
119
|
+
const res = loadRepoConfig(repo);
|
|
120
|
+
assert.equal(res.ok, true);
|
|
121
|
+
assert.equal(res.exists, false);
|
|
122
|
+
assert.deepEqual(res.config, {});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("loads a valid config", () => {
|
|
126
|
+
writeConfig(JSON.stringify({
|
|
127
|
+
indexDocs: false,
|
|
128
|
+
docExtensions: [".md"],
|
|
129
|
+
docPatterns: ["docs/", "README.md"],
|
|
130
|
+
ignoredDirs: ["sandbox"],
|
|
131
|
+
skipExtensions: [".snap"],
|
|
132
|
+
}));
|
|
133
|
+
const res = loadRepoConfig(repo);
|
|
134
|
+
assert.equal(res.ok, true);
|
|
135
|
+
assert.equal(res.exists, true);
|
|
136
|
+
assert.equal(res.config.indexDocs, false);
|
|
137
|
+
assert.deepEqual(res.config.docPatterns, ["docs/", "README.md"]);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("rejects malformed JSON with a clear error", () => {
|
|
141
|
+
writeConfig("{ not json");
|
|
142
|
+
const res = loadRepoConfig(repo);
|
|
143
|
+
assert.equal(res.ok, false);
|
|
144
|
+
assert.ok(res.error.includes("not valid JSON"), `error should mention JSON: ${res.error}`);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("rejects wrong types with a clear error", () => {
|
|
148
|
+
writeConfig(JSON.stringify({ indexDocs: "no", ignoredDirs: "sandbox", docPatterns: [1] }));
|
|
149
|
+
const res = loadRepoConfig(repo);
|
|
150
|
+
assert.equal(res.ok, false);
|
|
151
|
+
assert.ok(res.error.includes("indexDocs"), `error should mention indexDocs: ${res.error}`);
|
|
152
|
+
assert.ok(res.error.includes("ignoredDirs"), `error should mention ignoredDirs: ${res.error}`);
|
|
153
|
+
assert.ok(res.error.includes("docPatterns"), `error should mention docPatterns: ${res.error}`);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it("rejects non-object roots", () => {
|
|
157
|
+
writeConfig("[1,2,3]");
|
|
158
|
+
const res = validateRepoConfig("[1,2,3]");
|
|
159
|
+
assert.equal(res.ok, false);
|
|
160
|
+
assert.ok(res.error.includes("object"));
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it("configErrorText tells the AI client how to fix and retry", () => {
|
|
164
|
+
const text = configErrorText("/repo", "/repo/.yats/config.json", "not valid JSON: Unexpected token");
|
|
165
|
+
assert.ok(text.includes("Invalid repo config"), "names the problem");
|
|
166
|
+
assert.ok(text.includes("not valid JSON"), "includes the specific error");
|
|
167
|
+
assert.ok(text.includes("yats index /repo"), "gives the exact re-run command");
|
|
168
|
+
assert.ok(text.includes("--no-config"), "offers the bypass flag");
|
|
169
|
+
assert.ok(text.includes("Delete the file"), "offers falling back to defaults");
|
|
170
|
+
assert.ok(text.includes('"docPatterns"'), "shows the schema");
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
describe("mergeRepoConfig — machine vs repo (P6)", () => {
|
|
175
|
+
const globalEnv = {
|
|
176
|
+
DOC_EXTENSIONS: ".md,.rst",
|
|
177
|
+
SKIP_EXTENSIONS: ".lock,.min.js",
|
|
178
|
+
IGNORED_DIRS: "vendor,dist",
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
it("unions ignoredDirs and skipExtensions", () => {
|
|
182
|
+
const m = mergeRepoConfig(globalEnv, {
|
|
183
|
+
ignoredDirs: ["sandbox"],
|
|
184
|
+
skipExtensions: [".snap"],
|
|
185
|
+
});
|
|
186
|
+
assert.deepEqual(m.ignoredDirs, ["vendor", "dist", "sandbox"]);
|
|
187
|
+
assert.deepEqual(m.skipExtensions, [".lock", ".min.js", ".snap"]);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it("replaces docExtensions when the repo sets them (even empty)", () => {
|
|
191
|
+
const replaced = mergeRepoConfig(globalEnv, { docExtensions: [".mdx"] });
|
|
192
|
+
assert.deepEqual(replaced.docExtensions, [".mdx"]);
|
|
193
|
+
|
|
194
|
+
const emptied = mergeRepoConfig(globalEnv, { docExtensions: [] });
|
|
195
|
+
assert.deepEqual(emptied.docExtensions, [], "empty array disables docs for the repo");
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it("keeps global docExtensions when the repo does not set them", () => {
|
|
199
|
+
const m = mergeRepoConfig(globalEnv, {});
|
|
200
|
+
assert.deepEqual(m.docExtensions, [".md", ".rst"]);
|
|
201
|
+
assert.equal(m.docPatterns, null);
|
|
202
|
+
assert.equal(m.indexDocs, true);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it("repo indexDocs:false turns docs off", () => {
|
|
206
|
+
const m = mergeRepoConfig(globalEnv, { indexDocs: false });
|
|
207
|
+
assert.equal(m.indexDocs, false);
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
describe("shouldSkipFile — docPatterns whitelist (P6)", () => {
|
|
212
|
+
const docExtensions = [".md"];
|
|
213
|
+
|
|
214
|
+
it("only sends doc files matching a pattern (code files always sent)", () => {
|
|
215
|
+
const opts = { docExtensions, docPatterns: ["docs/", "README.md"] };
|
|
216
|
+
assert.equal(shouldSkipFile("docs/guide.md", opts), false, "docs/ matches");
|
|
217
|
+
assert.equal(shouldSkipFile("README.md", opts), false, "README.md matches");
|
|
218
|
+
assert.equal(shouldSkipFile("notes.md", opts), true, "notes.md is outside the whitelist");
|
|
219
|
+
assert.equal(shouldSkipFile("src/main.ts", opts), false, "code is never filtered by docPatterns");
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it("without patterns every doc file is sent", () => {
|
|
223
|
+
assert.equal(shouldSkipFile("notes.md", { docExtensions, docPatterns: null }), false);
|
|
224
|
+
});
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
describe("indexRepo — malformed config stops the run (P6)", () => {
|
|
228
|
+
let repo;
|
|
229
|
+
|
|
230
|
+
before(() => {
|
|
231
|
+
repo = join(tmpdir(), `yats-broken-config-test-${Date.now()}`);
|
|
232
|
+
mkdirSync(join(repo, ".yats"), { recursive: true });
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
after(() => {
|
|
236
|
+
try { rmSync(repo, { recursive: true }); } catch {}
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it("exits 1 with fix instructions before indexing anything", () => {
|
|
240
|
+
writeFileSync(join(repo, ".yats", "config.json"), "{ broken json");
|
|
241
|
+
try {
|
|
242
|
+
execFileSync(process.execPath, [SETUP_JS, "index", repo], {
|
|
243
|
+
encoding: "utf-8",
|
|
244
|
+
stdio: "pipe",
|
|
245
|
+
});
|
|
246
|
+
assert.fail("should have exited non-zero");
|
|
247
|
+
} catch (err) {
|
|
248
|
+
assert.equal(err.status, 1);
|
|
249
|
+
const out = `${err.stdout ?? ""}\n${err.stderr ?? ""}`;
|
|
250
|
+
assert.ok(out.includes("Invalid repo config"), out);
|
|
251
|
+
assert.ok(out.includes("yats index"), out);
|
|
252
|
+
assert.ok(out.includes("--no-config"), out);
|
|
253
|
+
// It must NOT even try to reach the server — config is checked first.
|
|
254
|
+
assert.ok(!out.includes("Cannot reach YATS"), out);
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it("a valid config proceeds (fails later on the unreachable server)", () => {
|
|
259
|
+
writeFileSync(join(repo, ".yats", "config.json"), JSON.stringify({ indexDocs: false }));
|
|
260
|
+
try {
|
|
261
|
+
execFileSync(process.execPath, [SETUP_JS, "index", repo], {
|
|
262
|
+
encoding: "utf-8",
|
|
263
|
+
stdio: "pipe",
|
|
264
|
+
env: { ...process.env, YATS_URL: "http://127.0.0.1:1" },
|
|
265
|
+
});
|
|
266
|
+
assert.fail("should have exited non-zero");
|
|
267
|
+
} catch (err) {
|
|
268
|
+
assert.equal(err.status, 1);
|
|
269
|
+
const out = `${err.stdout ?? ""}\n${err.stderr ?? ""}`;
|
|
270
|
+
assert.ok(!out.includes("Invalid repo config"), out);
|
|
271
|
+
assert.ok(out.includes("Cannot reach YATS"), out);
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
});
|
package/src/setup.js
CHANGED
|
@@ -133,9 +133,9 @@ const EMBEDDED_COMPOSE = `services:
|
|
|
133
133
|
- VOYAGE_MODEL=\${EMBEDDING_VOYAGE_MODEL:-voyage-code-2}
|
|
134
134
|
- REPOSITORIES_PATH=/repos
|
|
135
135
|
- YATS_PORT=__MCP_PORT__
|
|
136
|
-
- EMBEDDING_BATCH_SIZE
|
|
137
|
-
- INDEX_DOCS
|
|
138
|
-
- DOC_MAX_FILES
|
|
136
|
+
- EMBEDDING_BATCH_SIZE=\${EMBEDDING_BATCH_SIZE:-__BATCH_SIZE__}
|
|
137
|
+
- INDEX_DOCS=\${INDEX_DOCS:-__INDEX_DOCS__}
|
|
138
|
+
- DOC_MAX_FILES=\${DOC_MAX_FILES:-__DOC_MAX__}
|
|
139
139
|
- DOC_EXTENSIONS=\${DOC_EXTENSIONS:-__DOC_EXTENSIONS__}
|
|
140
140
|
- SKIP_EXTENSIONS=\${SKIP_EXTENSIONS:-__SKIP_EXTENSIONS__}
|
|
141
141
|
- IGNORED_DIRS=\${IGNORED_DIRS:-__IGNORED_DIRS__}
|