layermap 0.1.20 → 0.1.22
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/README.md +8 -4
- package/analyzers/project-map-java/java-map.jar +0 -0
- package/dist/cli.mjs +491 -118
- package/package.json +1 -1
- package/pi/layermap.mjs +123 -26
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "layermap",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.22",
|
|
4
4
|
"mcpName": "io.github.coffeecoproject/layermap",
|
|
5
5
|
"description": "A layered map of a codebase for coding agents: directories, modules and call chains across TypeScript, JavaScript, Go, Python and Java.",
|
|
6
6
|
"license": "Apache-2.0",
|
package/pi/layermap.mjs
CHANGED
|
@@ -7,14 +7,15 @@
|
|
|
7
7
|
// asks the agent once, before it settles, to check what its code edits affect.
|
|
8
8
|
import { execFile, spawn } from "node:child_process";
|
|
9
9
|
import { createHash } from "node:crypto";
|
|
10
|
-
import { stat } from "node:fs/promises";
|
|
10
|
+
import { readFile, stat } from "node:fs/promises";
|
|
11
|
+
import os from "node:os";
|
|
11
12
|
import path from "node:path";
|
|
12
13
|
import { createInterface } from "node:readline";
|
|
13
14
|
import { fileURLToPath } from "node:url";
|
|
14
15
|
import { Type } from "typebox";
|
|
15
16
|
|
|
16
17
|
const MAP_NOTE = "The layermap MCP server is available here. When a task needs to know what calls a function, what a change could affect beyond the code you are editing, or where code is used, use its project_explore_map tool (direction INCOMING, depth up to 8) instead of grep, then confirm in source; a change confined to code you have read does not need it. When the edits are done, call its project_check_changes tool once before reporting the work done.";
|
|
17
|
-
const STOP_CHECK = "Code changed in this session. Before finishing, call the LayerMap tool project_check_changes: it lists the routes, jobs and commands the changes reach and the existing tests related to them. Check the affected entry points in source, and run the related tests if the change needs verifying. If it lists EXISTING TESTS CHANGED or code SHARED BY several areas, decide which of them the request is about; for the others, restore their tests and narrow the change. If you already checked these changes, say so briefly and finish.";
|
|
18
|
+
const STOP_CHECK = "Code changed in this session. Before finishing, call the LayerMap tool project_check_changes: it lists the routes, jobs and commands the changes reach and the existing tests related to them. Check the affected entry points in source, and run the related tests if the change needs verifying. If it lists EXISTING TESTS CHANGED or code SHARED BY several areas, decide which of them the request is about; for the others, restore their tests and narrow the change. If it lists NEW MODULE DEPENDENCIES, check that each runs the way the project's layers do. If you already checked these changes, say so briefly and finish.";
|
|
18
19
|
const STOP_CHECK_REPOSITORIES = "The changes are in more than one repository, or not in the one the session started in: call project_check_changes once for each, with its path set to the repository: ";
|
|
19
20
|
const CHECK_TOOL = "project_check_changes";
|
|
20
21
|
const CLI = path.join(path.dirname(fileURLToPath(import.meta.url)), "..", "dist", "cli.mjs");
|
|
@@ -134,16 +135,67 @@ function routedRepository(start, given) {
|
|
|
134
135
|
return repositoryAt(path.resolve(start.directory, normal));
|
|
135
136
|
}
|
|
136
137
|
|
|
137
|
-
//
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
const
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
138
|
+
// A repository's uncommitted code files, each with a hash of its content ("" once deleted); empty
|
|
139
|
+
// without a commit.
|
|
140
|
+
async function changedCode(repository) {
|
|
141
|
+
const changed = new Map();
|
|
142
|
+
if (!(await git(repository, ["rev-parse", "--verify", "--quiet", "HEAD"]))) return changed;
|
|
143
|
+
const listed = await Promise.all([
|
|
144
|
+
git(repository, [
|
|
145
|
+
"-c",
|
|
146
|
+
"core.quotePath=false",
|
|
147
|
+
"diff",
|
|
148
|
+
"HEAD",
|
|
149
|
+
"--name-only",
|
|
150
|
+
"--no-renames",
|
|
151
|
+
"--",
|
|
152
|
+
...CODE_GLOBS,
|
|
153
|
+
]),
|
|
154
|
+
git(repository, [
|
|
155
|
+
"-c",
|
|
156
|
+
"core.quotePath=false",
|
|
157
|
+
"ls-files",
|
|
158
|
+
"--others",
|
|
159
|
+
"--exclude-standard",
|
|
160
|
+
"--",
|
|
161
|
+
...CODE_GLOBS,
|
|
162
|
+
]),
|
|
163
|
+
]);
|
|
164
|
+
const files = [...new Set(listed.join("\n").split("\n").filter(Boolean))].sort();
|
|
165
|
+
for (const file of files) {
|
|
166
|
+
const content = await readFile(path.join(repository, file)).catch(() => undefined);
|
|
167
|
+
changed.set(file, content ? createHash("sha256").update(content).digest("hex") : "");
|
|
168
|
+
}
|
|
169
|
+
return changed;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// The changes as one hash, empty when there are none.
|
|
173
|
+
const fingerprint = (changed) =>
|
|
174
|
+
changed.size
|
|
175
|
+
? createHash("sha256")
|
|
176
|
+
.update(JSON.stringify([...changed]))
|
|
177
|
+
.digest("hex")
|
|
178
|
+
: "";
|
|
179
|
+
|
|
180
|
+
// How long a command runs (ms) before only the files it names count as its changes.
|
|
181
|
+
const LONG_COMMAND = 60_000;
|
|
182
|
+
|
|
183
|
+
// The folders a shell command names that exist, relative to the directory it runs in.
|
|
184
|
+
async function namedFolders(cwd, command) {
|
|
185
|
+
const folders = new Set();
|
|
186
|
+
for (const word of command.split(/[^A-Za-z0-9._/~:@+,%-]+/).slice(0, 300)) {
|
|
187
|
+
if (!word) continue;
|
|
188
|
+
const named = word.startsWith("~/")
|
|
189
|
+
? path.join(os.homedir(), word.slice(2))
|
|
190
|
+
: path.resolve(cwd, word);
|
|
191
|
+
for (const folder of [named, path.dirname(named)])
|
|
192
|
+
if ((await stat(folder).catch(() => undefined))?.isDirectory()) {
|
|
193
|
+
folders.add(folder);
|
|
194
|
+
break;
|
|
195
|
+
}
|
|
196
|
+
if (folders.size >= 20) break;
|
|
197
|
+
}
|
|
198
|
+
return folders;
|
|
147
199
|
}
|
|
148
200
|
|
|
149
201
|
export default function layermap(pi) {
|
|
@@ -154,22 +206,28 @@ export default function layermap(pi) {
|
|
|
154
206
|
// Where the server resolves relative paths from: the repository the session started in, or its
|
|
155
207
|
// directory outside one.
|
|
156
208
|
let start = { directory: "", git: false };
|
|
157
|
-
// The stop check looks at each repository the agent
|
|
158
|
-
//
|
|
159
|
-
// session started in
|
|
209
|
+
// The stop check looks at each repository the agent changed code in: those of the files an
|
|
210
|
+
// edit, write or patch names, and those in which any other tool that can change files (bash, …)
|
|
211
|
+
// changed code files while it ran, among the repository the session started in, those of the
|
|
212
|
+
// folders its command names and those already edited.
|
|
160
213
|
// It compares a repository's uncommitted code changes with those it had before the session's
|
|
161
214
|
// first such tool there and those the agent last checked in it, and all of them with those it
|
|
162
|
-
// was last asked about; changes another session makes while this one reads do not count.
|
|
215
|
+
// was last asked about; changes another session makes while this one reads do not count. A
|
|
216
|
+
// command that ran longer than a minute (tests, builds, scripts) leaves time for another session
|
|
217
|
+
// to change the same work tree, so only the files it names count.
|
|
163
218
|
const edited = new Set();
|
|
164
219
|
const before = new Map();
|
|
165
220
|
const checked = new Map();
|
|
221
|
+
// The code changes of each repository a running tool may change, when it started and its
|
|
222
|
+
// command, by the tool call's id.
|
|
223
|
+
const running = new Map();
|
|
166
224
|
let askedAbout = "";
|
|
167
225
|
|
|
168
226
|
const ensureServer = async () => {
|
|
169
227
|
if (server && !server.dead) return server;
|
|
170
228
|
starting ??= (async () => {
|
|
171
229
|
const next = new MapServer(cwd);
|
|
172
|
-
const tools = await next.start("0.1.
|
|
230
|
+
const tools = await next.start("0.1.22");
|
|
173
231
|
for (const tool of tools) {
|
|
174
232
|
if (registered.has(tool.name)) continue;
|
|
175
233
|
registered.add(tool.name);
|
|
@@ -191,7 +249,7 @@ export default function layermap(pi) {
|
|
|
191
249
|
if (result.isError) throw new Error(text);
|
|
192
250
|
if (tool.name === CHECK_TOOL) {
|
|
193
251
|
const repository = await routedRepository(start, params?.path);
|
|
194
|
-
if (repository) checked.set(repository, await
|
|
252
|
+
if (repository) checked.set(repository, fingerprint(await changedCode(repository)));
|
|
195
253
|
}
|
|
196
254
|
return { content: [{ type: "text", text }], details: {} };
|
|
197
255
|
},
|
|
@@ -212,6 +270,7 @@ export default function layermap(pi) {
|
|
|
212
270
|
edited.clear();
|
|
213
271
|
before.clear();
|
|
214
272
|
checked.clear();
|
|
273
|
+
running.clear();
|
|
215
274
|
askedAbout = "";
|
|
216
275
|
await ensureServer().catch((error) =>
|
|
217
276
|
ctx.ui?.notify?.(`LayerMap could not start: ${error.message}`, "warning"),
|
|
@@ -221,13 +280,51 @@ export default function layermap(pi) {
|
|
|
221
280
|
pi.on("tool_call", async (event) => {
|
|
222
281
|
if (READS.has(event.toolName) || registered.has(event.toolName) || !cwd) return;
|
|
223
282
|
const files = namedFiles(event);
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
283
|
+
if (files.length) {
|
|
284
|
+
const repositories = await Promise.all(
|
|
285
|
+
files.map((file) => repositoryAt(path.resolve(cwd, file))),
|
|
286
|
+
);
|
|
287
|
+
for (const repository of new Set(repositories)) {
|
|
288
|
+
if (!repository || edited.has(repository)) continue;
|
|
289
|
+
edited.add(repository);
|
|
290
|
+
before.set(repository, fingerprint(await changedCode(repository)));
|
|
291
|
+
}
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
const command = event.input?.command;
|
|
295
|
+
const folders = typeof command === "string" ? await namedFolders(cwd, command) : [];
|
|
296
|
+
const repositories = new Set([
|
|
297
|
+
...(start.git ? [start.directory] : []),
|
|
298
|
+
...(await Promise.all([...folders].map(repositoryAt))),
|
|
299
|
+
...edited,
|
|
300
|
+
]);
|
|
301
|
+
const changes = new Map();
|
|
302
|
+
for (const repository of repositories)
|
|
303
|
+
if (repository) changes.set(repository, await changedCode(repository));
|
|
304
|
+
running.set(event.toolCallId, {
|
|
305
|
+
changes,
|
|
306
|
+
started: Date.now(),
|
|
307
|
+
command: typeof command === "string" ? command : "",
|
|
308
|
+
});
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
pi.on("tool_result", async (event) => {
|
|
312
|
+
const call = running.get(event.toolCallId);
|
|
313
|
+
if (!call) return;
|
|
314
|
+
running.delete(event.toolCallId);
|
|
315
|
+
const long = Date.now() - call.started > LONG_COMMAND;
|
|
316
|
+
for (const [repository, earlier] of call.changes) {
|
|
317
|
+
if (edited.has(repository)) continue;
|
|
318
|
+
const now = await changedCode(repository);
|
|
319
|
+
if (
|
|
320
|
+
[...now].some(
|
|
321
|
+
([file, content]) =>
|
|
322
|
+
earlier.get(file) !== content && (!long || call.command.includes(path.basename(file))),
|
|
323
|
+
)
|
|
324
|
+
) {
|
|
325
|
+
edited.add(repository);
|
|
326
|
+
before.set(repository, fingerprint(earlier));
|
|
327
|
+
}
|
|
231
328
|
}
|
|
232
329
|
});
|
|
233
330
|
|
|
@@ -235,7 +332,7 @@ export default function layermap(pi) {
|
|
|
235
332
|
if (process.env.LAYERMAP_STOP_CHECK === "0") return;
|
|
236
333
|
const due = [];
|
|
237
334
|
for (const repository of edited) {
|
|
238
|
-
const changes = await
|
|
335
|
+
const changes = fingerprint(await changedCode(repository));
|
|
239
336
|
if (changes && changes !== before.get(repository) && changes !== checked.get(repository))
|
|
240
337
|
due.push({ repository, changes });
|
|
241
338
|
}
|