layermap 0.1.36 → 0.1.38

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/dist/cli.mjs CHANGED
@@ -291,6 +291,7 @@ import { createHash as createHash5, randomUUID as randomUUID2 } from "node:crypt
291
291
  import { mkdirSync, statSync } from "node:fs";
292
292
  import { lstat as lstat3, realpath as realpath2 } from "node:fs/promises";
293
293
  import path12 from "node:path";
294
+ import { setTimeout as delay } from "node:timers/promises";
294
295
  import { DatabaseSync } from "@photostructure/sqlite";
295
296
 
296
297
  // src/capture-policy.ts
@@ -22183,16 +22184,19 @@ ${scope}`, [
22183
22184
 
22184
22185
  // src/code-index.ts
22185
22186
  var CodeIndex = class {
22186
- constructor(store, clock = () => (/* @__PURE__ */ new Date()).toISOString(), analyzer = new ProjectMapAnalyzer()) {
22187
+ constructor(store, clock = () => (/* @__PURE__ */ new Date()).toISOString(), analyzer = new ProjectMapAnalyzer(), lockBuilds = async () => () => {
22188
+ }) {
22187
22189
  this.store = store;
22188
22190
  this.clock = clock;
22189
22191
  this.analyzer = analyzer;
22192
+ this.lockBuilds = lockBuilds;
22190
22193
  this.query = new CodeIndexQuery(store, analyzer.id, (signal, read) => this.read(signal, read));
22191
22194
  this.capture = new CodeIndexCapture(analyzer.id);
22192
22195
  }
22193
22196
  store;
22194
22197
  clock;
22195
22198
  analyzer;
22199
+ lockBuilds;
22196
22200
  query;
22197
22201
  capture;
22198
22202
  captures = /* @__PURE__ */ new Map();
@@ -22283,7 +22287,14 @@ var CodeIndex = class {
22283
22287
  this.store.discardMapStage(stage);
22284
22288
  }
22285
22289
  };
22286
- const completion = run2(this.mapParent(result, startFromProjectRef)).finally(() => {
22290
+ const completion = (async () => {
22291
+ const release = await this.lockBuilds(controller.signal);
22292
+ try {
22293
+ if (!this.store.getVersion(ref)) await run2(this.mapParent(result, startFromProjectRef));
22294
+ } finally {
22295
+ release();
22296
+ }
22297
+ })().finally(() => {
22287
22298
  if (this.mapBuilds.get(ref)?.completion === completion) this.mapBuilds.delete(ref);
22288
22299
  });
22289
22300
  const build = { controller, completion, waiters: /* @__PURE__ */ new Set() };
@@ -23135,7 +23146,14 @@ async function readMapChanges(project, base, signal) {
23135
23146
  };
23136
23147
  const untracked = (await git.run(["ls-files", "--others", "--exclude-standard", "-z"], gitOptions(project, signal))).stdout.toString("utf8").split("\0").filter((path17) => path17 && !inExcludedDirectory(path17));
23137
23148
  for (const path17 of untracked)
23138
- files.push({ path: path17, status: "ADDED", lines: [EVERYTHING], baseLines: [], binary: false });
23149
+ files.push({
23150
+ path: path17,
23151
+ status: "ADDED",
23152
+ lines: [EVERYTHING],
23153
+ baseLines: [],
23154
+ hunks: [],
23155
+ binary: false
23156
+ });
23139
23157
  const stranded = await strandedNames(
23140
23158
  project,
23141
23159
  diff,
@@ -23164,6 +23182,7 @@ function parseMapDiff(text) {
23164
23182
  status: "MODIFIED",
23165
23183
  lines: [],
23166
23184
  baseLines: [],
23185
+ hunks: [],
23167
23186
  binary: false
23168
23187
  };
23169
23188
  continue;
@@ -23189,6 +23208,12 @@ function parseMapDiff(text) {
23189
23208
  Number(hunk[3]),
23190
23209
  hunk[4] === void 0 ? 1 : Number(hunk[4])
23191
23210
  ];
23211
+ current.hunks.push({
23212
+ baseStart: oldStart,
23213
+ baseCount: oldCount,
23214
+ start: newStart,
23215
+ count: newCount
23216
+ });
23192
23217
  if (oldCount > 0) current.baseLines.push([oldStart, oldStart + oldCount - 1]);
23193
23218
  current.lines.push(
23194
23219
  newCount > 0 ? [newStart, newStart + newCount - 1] : [newStart + 1, newStart]
@@ -23200,6 +23225,14 @@ function parseMapDiff(text) {
23200
23225
  }
23201
23226
  var testCode = (file2) => mapTestPath(file2) || /(?:^|\/)testFixtures\//u.test(file2);
23202
23227
  var overlaps = (lines, start, end) => lines.some(([from, to]) => from <= end && start <= to);
23228
+ var workingLine = (file2, line) => {
23229
+ let shift = 0;
23230
+ for (const hunk of file2?.hunks ?? []) {
23231
+ if ((hunk.baseCount ? hunk.baseStart : hunk.baseStart + 1) > line) break;
23232
+ shift += hunk.count - hunk.baseCount;
23233
+ }
23234
+ return line + shift;
23235
+ };
23203
23236
  var NON_CODE = [
23204
23237
  [
23205
23238
  /(?:^|\/)(?:package-lock\.json|pnpm-lock\.yaml|yarn\.lock|go\.sum|poetry\.lock|Pipfile\.lock|uv\.lock|Cargo\.lock|gradle\.lockfile)$/u,
@@ -23746,18 +23779,35 @@ async function mapChangeImpact(store, working, base, changes, shapes, until, pro
23746
23779
  const changedIn = new Map(
23747
23780
  changes.files.flatMap((file2) => file2.basePath ? [[file2.basePath, file2]] : [])
23748
23781
  );
23749
- const same2 = (g, ref) => `${g.object(ref).kind}\0${g.qualifiedName(ref)}`;
23750
- const stillUses = (relation, user) => {
23751
- const file2 = changedIn.get(relation.anchor.path);
23752
- if (file2 && (file2.status === "DELETED" || overlaps(file2.baseLines, relation.anchor.startLine, relation.anchor.endLine)))
23753
- return false;
23754
- const name = same2(before, user);
23755
- const now = graph.objectsAt(file2?.path ?? relation.anchor.path).find((object2) => same2(graph, object2.id) === name);
23756
- if (!now || relation.to === void 0) return true;
23757
- const used = same2(before, relation.to);
23758
- const tag = mapIsModule(before.object(relation.to));
23759
- return !graph.relationsFrom(graph.scope(now.id).refs).some(
23760
- (use) => NAMING_KINDS.has(use.kind) && use.to !== void 0 && (tag ? use.target === relation.target : same2(graph, use.to) === used)
23782
+ const nowAt = (anchor) => {
23783
+ const file2 = changedIn.get(anchor.path);
23784
+ if (file2 && (file2.status === "DELETED" || overlaps(file2.baseLines, anchor.startLine, anchor.endLine)))
23785
+ return void 0;
23786
+ return {
23787
+ path: file2?.path ?? anchor.path,
23788
+ startLine: workingLine(file2, anchor.startLine),
23789
+ endLine: workingLine(file2, anchor.endLine)
23790
+ };
23791
+ };
23792
+ const samePlace = (anchor, place) => anchor.path === place.path && anchor.startLine === place.startLine && anchor.endLine === place.endLine;
23793
+ const replaces = (now, removed2) => now.kind === removed2.kind && now.name === removed2.name;
23794
+ const relationsNow = /* @__PURE__ */ new Map();
23795
+ const relationsIn = (path17) => {
23796
+ let found = relationsNow.get(path17);
23797
+ if (!found) {
23798
+ found = graph.relationsFrom(graph.objectsAt(path17).map((object2) => object2.id));
23799
+ relationsNow.set(path17, found);
23800
+ }
23801
+ return found;
23802
+ };
23803
+ const stillUses = (relation) => {
23804
+ const place = nowAt(relation.anchor);
23805
+ if (!place) return false;
23806
+ if (relation.to === void 0) return true;
23807
+ const used = before.object(relation.to);
23808
+ const tag = mapIsModule(used);
23809
+ return !relationsIn(place.path).some(
23810
+ (use) => NAMING_KINDS.has(use.kind) && use.to !== void 0 && samePlace(use.anchor, place) && (tag ? use.target === relation.target : replaces(graph.object(use.to), used))
23761
23811
  );
23762
23812
  };
23763
23813
  const gone = [];
@@ -23786,8 +23836,7 @@ async function mapChangeImpact(store, working, base, changes, shapes, until, pro
23786
23836
  before.objectsAt(file2.basePath).filter((member) => inside(member.id)).map((member) => member.id)
23787
23837
  )) {
23788
23838
  const owner = before.owner(relation.from);
23789
- if (!NAMING_KINDS.has(relation.kind) || inside(owner) || !stillUses(relation, owner))
23790
- continue;
23839
+ if (!NAMING_KINDS.has(relation.kind) || inside(owner) || !stillUses(relation)) continue;
23791
23840
  const user = before.object(owner);
23792
23841
  usedBy.add(
23793
23842
  mapIsModule(user) ? `${user.anchor.path}${mapMiniprogramTemplate(user.anchor.path) ? " (template)" : ": top-level code"}` : `${user.anchor.path}: ${before.qualifiedName(owner)}`
@@ -23805,28 +23854,56 @@ async function mapChangeImpact(store, working, base, changes, shapes, until, pro
23805
23854
  }
23806
23855
  }
23807
23856
  await beforeTracer.shapes.read(goneShapes.flatMap((item) => item.shapes));
23808
- const nowDeclared = new Set(
23809
- changes.files.flatMap(
23810
- (file2) => file2.status === "DELETED" ? [] : graph.objectsAt(file2.path).map((object2) => same2(graph, object2.id))
23857
+ const declared = /* @__PURE__ */ new Map();
23858
+ for (const file2 of changes.files)
23859
+ if (file2.status !== "DELETED") {
23860
+ for (const object2 of graph.objectsAt(file2.path))
23861
+ if (mapIsShape(object2)) {
23862
+ const key = `${object2.kind}\0${object2.name}`;
23863
+ declared.set(key, [...declared.get(key) ?? [], object2]);
23864
+ }
23865
+ }
23866
+ const successors = (shape) => {
23867
+ const removed2 = before.object(shape);
23868
+ return declared.get(`${removed2.kind}\0${removed2.name}`) ?? [];
23869
+ };
23870
+ await tracer.shapes.read(
23871
+ goneShapes.flatMap(
23872
+ (item) => item.shapes.flatMap((shape) => successors(shape).map((object2) => object2.id))
23811
23873
  )
23812
23874
  );
23875
+ const unfollowed = /* @__PURE__ */ new Set();
23813
23876
  for (const { shapes: inRemoved, inside, usedBy } of goneShapes)
23814
23877
  for (const shape of inRemoved) {
23815
- if (nowDeclared.has(same2(before, shape))) continue;
23816
23878
  if (beforeTracer.shapes.unread.has(shape))
23817
23879
  unclear.push(
23818
23880
  `${before.object(shape).anchor.path}: ${before.qualifiedName(shape)} was removed, and who used it was not read; search the code for its name`
23819
23881
  );
23882
+ const now = successors(shape);
23820
23883
  for (const { owner, anchor } of beforeTracer.shapes.uses.get(shape) ?? []) {
23821
- const file2 = changedIn.get(anchor.path);
23822
- if (inside(owner) || file2 && (file2.status === "DELETED" || overlaps(file2.baseLines, anchor.startLine, anchor.endLine)))
23884
+ const place = nowAt(anchor);
23885
+ if (inside(owner) || !place || now.some(
23886
+ (object2) => tracer.shapes.uses.get(object2.id)?.some((use) => samePlace(use.anchor, place))
23887
+ ))
23888
+ continue;
23889
+ if (now.some((object2) => tracer.shapes.unread.has(object2.id))) {
23890
+ unfollowed.add(`${before.object(shape).anchor.path}: ${before.qualifiedName(shape)}`);
23823
23891
  continue;
23892
+ }
23824
23893
  const user = before.object(owner);
23825
23894
  usedBy.add(
23826
23895
  mapIsModule(user) ? `${user.anchor.path}: top-level code` : `${user.anchor.path}: ${before.qualifiedName(owner)}`
23827
23896
  );
23828
23897
  }
23829
23898
  }
23899
+ if (unfollowed.size)
23900
+ unclear.push(
23901
+ `${unfollowed.size} removed fields or types are used on lines the diff left alone, and whether that code now uses what took their place was not read (${[
23902
+ ...unfollowed
23903
+ ].sort().slice(0, LIST.names).join(
23904
+ ", "
23905
+ )}${unfollowed.size > LIST.names ? ", \u2026" : ""}); project_find_references lists their uses`
23906
+ );
23830
23907
  const removedAt = (ref) => {
23831
23908
  for (let at = ref; at !== void 0; at = before.parent(at))
23832
23909
  if (gone.includes(at)) return true;
@@ -23877,7 +23954,11 @@ async function mapChangeImpact(store, working, base, changes, shapes, until, pro
23877
23954
  if (gone.length) {
23878
23955
  await beforeTracer.trace(gone, gone);
23879
23956
  for (const entry of beforeTracer.entries.values())
23880
- if (!tracer.entries.has(entry.label)) tracer.entries.set(entry.label, entry);
23957
+ if (!tracer.entries.has(entry.label))
23958
+ tracer.entries.set(
23959
+ entry.label,
23960
+ entry.route && removedRoutes.includes(entry.label) ? { ...entry, removed: true } : entry
23961
+ );
23881
23962
  for (const [path17, test] of beforeTracer.tests) {
23882
23963
  const known = tracer.tests.get(path17);
23883
23964
  if (known) for (const name of test.names) known.names.add(name);
@@ -24460,7 +24541,7 @@ Nothing changed against ${changes.base}.`;
24460
24541
  "Use the existing code rather than a copy, or, if the new code replaces it, remove the old; keep both only when they are meant to differ. Code that does the same in other words is not found."
24461
24542
  );
24462
24543
  }
24463
- const entry = (item) => `${item.route ? JSON.stringify(item.label) : item.label}${item.chain.length ? ` \u2192 ${item.chain.join(" \u2192 ")}` : ""}${item.site ? ` (registered at ${item.site})` : ""}`;
24544
+ const entry = (item) => `${item.route ? JSON.stringify(item.label) : item.label}${item.chain.length ? ` \u2192 ${item.chain.join(" \u2192 ")}` : ""}${item.removed ? ` (the diff removes it${item.site ? `; registered at ${item.site} before` : ""})` : item.site ? ` (registered at ${item.site})` : ""}`;
24464
24545
  list(
24465
24546
  "AFFECTED ENTRY POINTS (through resolved calls)",
24466
24547
  impact.entries.filter((e) => e.certain).map(entry),
@@ -24557,6 +24638,8 @@ var HOLD_EXPIRY_MS = 10 * 60 * 1e3;
24557
24638
  var BUSY_TIMEOUT = "PRAGMA busy_timeout = 30000;";
24558
24639
  var JOURNAL_LIMIT = 32 * 1024 * 1024;
24559
24640
  var CAPTURE_ATTEMPTS = 3;
24641
+ var BUILD_WAIT_MS = 500;
24642
+ var SQLITE_BUSY = 5;
24560
24643
  var CAPTURE_AGAIN = /* @__PURE__ */ new Set([
24561
24644
  "PROJECT_FILE_CHANGED",
24562
24645
  "PROJECT_FILE_UNIVERSE_CHANGED",
@@ -24617,6 +24700,26 @@ function openMapDatabase(file2) {
24617
24700
  throw error51;
24618
24701
  }
24619
24702
  }
24703
+ async function lockMapBuilds(file2, signal) {
24704
+ const lock = new DatabaseSync(file2);
24705
+ try {
24706
+ lock.exec("PRAGMA busy_timeout = 0;");
24707
+ for (; ; ) {
24708
+ try {
24709
+ lock.exec("BEGIN EXCLUSIVE");
24710
+ return () => lock.close();
24711
+ } catch (error51) {
24712
+ if (error51.errcode !== SQLITE_BUSY) throw error51;
24713
+ }
24714
+ await delay(BUILD_WAIT_MS, void 0, { signal }).catch(() => {
24715
+ throw new ProjectEvidenceError("PROJECT_READ_CANCELLED", true);
24716
+ });
24717
+ }
24718
+ } catch (error51) {
24719
+ lock.close();
24720
+ throw error51;
24721
+ }
24722
+ }
24620
24723
  var mapDatabasePath = (cacheDirectory2, canonicalProject) => path12.join(
24621
24724
  cacheDirectory2,
24622
24725
  "projects",
@@ -24671,7 +24774,12 @@ var LayerMap = class _LayerMap {
24671
24774
  project,
24672
24775
  source,
24673
24776
  store,
24674
- new CodeIndex(store, void 0, analyzer),
24777
+ new CodeIndex(
24778
+ store,
24779
+ void 0,
24780
+ analyzer,
24781
+ (signal) => lockMapBuilds(path12.join(path12.dirname(file2), "build.lock"), signal)
24782
+ ),
24675
24783
  analyzer.id,
24676
24784
  { maxBytes: options.maxOutputBytes ?? 96 * 1024, envelopeBytes: 0 },
24677
24785
  `${file2}-wal`
@@ -25102,7 +25210,7 @@ async function pruneNpxInstalls(npx, installed, now) {
25102
25210
 
25103
25211
  // src/agent/mcp.ts
25104
25212
  import { createInterface } from "node:readline";
25105
- import { setTimeout as delay } from "node:timers/promises";
25213
+ import { setTimeout as delay2 } from "node:timers/promises";
25106
25214
  import { fileURLToPath as fileURLToPath2 } from "node:url";
25107
25215
 
25108
25216
  // src/agent/notes.ts
@@ -25388,7 +25496,7 @@ function serveMcp(options) {
25388
25496
  const id2 = `layermap-roots-${++requested}`;
25389
25497
  const reply = new Promise((resolve) => replies.set(id2, resolve));
25390
25498
  send({ id: id2, method: "roots/list" });
25391
- rooted = Promise.race([reply, delay(ROOTS_WAIT_MS, void 0, { ref: false })]).then(async (message) => {
25499
+ rooted = Promise.race([reply, delay2(ROOTS_WAIT_MS, void 0, { ref: false })]).then(async (message) => {
25392
25500
  replies.delete(id2);
25393
25501
  const roots = message?.result?.roots;
25394
25502
  const root = Array.isArray(roots) ? roots.find((item) => typeof item?.uri === "string" && item.uri.startsWith("file:")) : void 0;
@@ -25449,7 +25557,7 @@ ${text}` : text
25449
25557
  const timer = new AbortController();
25450
25558
  const ready = await Promise.race([
25451
25559
  built.then(() => true),
25452
- delay(options.buildWaitMs ?? 4e4, false, {
25560
+ delay2(options.buildWaitMs ?? 4e4, false, {
25453
25561
  signal: AbortSignal.any([timer.signal, controller.signal])
25454
25562
  }).catch(() => false)
25455
25563
  ]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "layermap",
3
- "version": "0.1.36",
3
+ "version": "0.1.38",
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
@@ -293,7 +293,7 @@ function layermap(pi) {
293
293
  if (server && !server.dead) return server;
294
294
  starting ??= (async () => {
295
295
  const next = new MapServer(cwd);
296
- const tools = await next.start("0.1.36");
296
+ const tools = await next.start("0.1.38");
297
297
  for (const tool of tools) {
298
298
  if (registered.has(tool.name)) continue;
299
299
  registered.add(tool.name);