devlensio 1.1.1 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/dist/graph/buildLookup.d.ts +30 -0
  2. package/dist/graph/buildLookup.js +36 -1
  3. package/dist/graph/edges/aliasChain.d.ts +7 -0
  4. package/dist/graph/edges/aliasChain.js +63 -0
  5. package/dist/graph/edges/aliasResolution.test.d.ts +1 -0
  6. package/dist/graph/edges/aliasResolution.test.js +351 -0
  7. package/dist/graph/edges/callEdges.js +178 -18
  8. package/dist/graph/edges/callEdges.test.js +119 -0
  9. package/dist/graph/edges/callOrder.test.d.ts +1 -0
  10. package/dist/graph/edges/callOrder.test.js +227 -0
  11. package/dist/graph/edges/importEdges.js +124 -0
  12. package/dist/graph/edges/propEdges.js +6 -1
  13. package/dist/index.d.ts +1 -0
  14. package/dist/index.js +1 -0
  15. package/dist/parser/callFlow.d.ts +15 -0
  16. package/dist/parser/callFlow.js +302 -0
  17. package/dist/parser/callOrder.test.d.ts +1 -0
  18. package/dist/parser/callOrder.test.js +407 -0
  19. package/dist/parser/declarationIds.d.ts +23 -0
  20. package/dist/parser/declarationIds.js +123 -0
  21. package/dist/parser/exportsMap.js +1 -90
  22. package/dist/parser/extractors/components.js +18 -28
  23. package/dist/parser/extractors/functions.d.ts +21 -1
  24. package/dist/parser/extractors/functions.js +92 -60
  25. package/dist/parser/extractors/moduleScope.d.ts +8 -0
  26. package/dist/parser/extractors/moduleScope.js +80 -0
  27. package/dist/parser/index.js +12 -0
  28. package/dist/parser/moduleScope.test.d.ts +1 -0
  29. package/dist/parser/moduleScope.test.js +176 -0
  30. package/dist/summarizer/index.js +53 -4
  31. package/dist/summarizer/prompts.js +111 -50
  32. package/dist/summarizer/prompts.test.d.ts +1 -0
  33. package/dist/summarizer/prompts.test.js +112 -0
  34. package/dist/types.d.ts +63 -0
  35. package/dist/version.d.ts +1 -0
  36. package/dist/version.js +2 -0
  37. package/extractors/go/bin/darwin-amd64/devlens_go_extractor +0 -0
  38. package/extractors/go/bin/darwin-arm64/devlens_go_extractor +0 -0
  39. package/extractors/go/bin/linux-amd64/devlens_go_extractor +0 -0
  40. package/extractors/go/bin/linux-arm64/devlens_go_extractor +0 -0
  41. package/extractors/go/bin/windows-amd64/devlens_go_extractor.exe +0 -0
  42. package/extractors/go/calls.go +131 -10
  43. package/extractors/go/nodes.go +29 -11
  44. package/extractors/go/parser.go +376 -41
  45. package/extractors/java/devlens_java_extractor.jar +0 -0
  46. package/extractors/java/src/devlens/extractor/Extractor.java +52 -12
  47. package/extractors/java/src/devlens/extractor/LookupMaps.java +2 -0
  48. package/extractors/java/src/devlens/extractor/Parser.java +420 -1
  49. package/extractors/java/src/devlens/extractor/edges/Calls.java +105 -5
  50. package/extractors/python/devlens_extractors_python/edges/calls.py +199 -36
  51. package/extractors/python/devlens_extractors_python/edges/imports.py +21 -1
  52. package/extractors/python/devlens_extractors_python/lookup.py +9 -0
  53. package/extractors/python/devlens_extractors_python/parser/__init__.py +11 -1
  54. package/extractors/python/devlens_extractors_python/parser/functions.py +199 -42
  55. package/extractors/rust/bin/darwin-amd64/devlens_rust_extractor +0 -0
  56. package/extractors/rust/bin/darwin-arm64/devlens_rust_extractor +0 -0
  57. package/extractors/rust/bin/linux-amd64/devlens_rust_extractor +0 -0
  58. package/extractors/rust/bin/linux-arm64/devlens_rust_extractor +0 -0
  59. package/extractors/rust/bin/windows-amd64/devlens_rust_extractor.exe +0 -0
  60. package/extractors/rust/src/calls.rs +114 -8
  61. package/extractors/rust/src/lookup.rs +66 -0
  62. package/extractors/rust/src/nodes.rs +29 -0
  63. package/extractors/rust/src/parser.rs +362 -27
  64. package/package.json +77 -73
@@ -10,6 +10,7 @@ import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
10
10
  import com.github.javaparser.ast.body.ConstructorDeclaration;
11
11
  import com.github.javaparser.ast.body.EnumDeclaration;
12
12
  import com.github.javaparser.ast.body.FieldDeclaration;
13
+ import com.github.javaparser.ast.body.InitializerDeclaration;
13
14
  import com.github.javaparser.ast.body.MethodDeclaration;
14
15
  import com.github.javaparser.ast.body.Parameter;
15
16
  import com.github.javaparser.ast.body.RecordDeclaration;
@@ -18,8 +19,12 @@ import com.github.javaparser.ast.body.VariableDeclarator;
18
19
  import com.github.javaparser.ast.nodeTypes.NodeWithMembers;
19
20
  import com.github.javaparser.ast.type.ReferenceType;
20
21
  import com.github.javaparser.ast.expr.AnnotationExpr;
22
+ import com.github.javaparser.ast.expr.CastExpr;
23
+ import com.github.javaparser.ast.expr.ConditionalExpr;
24
+ import com.github.javaparser.ast.expr.EnclosedExpr;
21
25
  import com.github.javaparser.ast.expr.Expression;
22
26
  import com.github.javaparser.ast.expr.FieldAccessExpr;
27
+ import com.github.javaparser.ast.expr.LambdaExpr;
23
28
  import com.github.javaparser.ast.expr.MethodCallExpr;
24
29
  import com.github.javaparser.ast.expr.NameExpr;
25
30
  import com.github.javaparser.ast.expr.NormalAnnotationExpr;
@@ -28,12 +33,22 @@ import com.github.javaparser.ast.expr.SingleMemberAnnotationExpr;
28
33
  import com.github.javaparser.ast.expr.StringLiteralExpr;
29
34
  import com.github.javaparser.ast.nodeTypes.NodeWithAnnotations;
30
35
  import com.github.javaparser.ast.stmt.BlockStmt;
36
+ import com.github.javaparser.ast.stmt.CatchClause;
37
+ import com.github.javaparser.ast.stmt.DoStmt;
38
+ import com.github.javaparser.ast.stmt.ForEachStmt;
39
+ import com.github.javaparser.ast.stmt.ForStmt;
40
+ import com.github.javaparser.ast.stmt.IfStmt;
41
+ import com.github.javaparser.ast.stmt.SwitchEntry;
42
+ import com.github.javaparser.ast.stmt.TryStmt;
43
+ import com.github.javaparser.ast.stmt.WhileStmt;
31
44
  import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration;
32
45
 
33
46
  import java.io.IOException;
34
47
  import java.nio.file.Files;
35
48
  import java.nio.file.Path;
36
49
  import java.util.ArrayList;
50
+ import java.util.Comparator;
51
+ import java.util.IdentityHashMap;
37
52
  import java.util.LinkedHashMap;
38
53
  import java.util.LinkedHashSet;
39
54
  import java.util.List;
@@ -79,6 +94,22 @@ public final class Parser {
79
94
  public int argCount; // argument count at the call site
80
95
  public List<String> argTypes = new ArrayList<>(); // cheap arg type tags ("String", "int", "unknown")
81
96
  public boolean hasSpread = false; // Java has no call-site spread; kept for contract parity
97
+
98
+ // ── callOrder/v1 (docs/CALL_ORDER.md) ────────────────────────────
99
+ // Metadata only: the SET of recorded calls is decided by the walk,
100
+ // these fields never add or remove a site.
101
+ public int order; // 1-based lexical rank among the owner's sites
102
+ public int col; // 0-based column of the call expression
103
+ public Integer nested; // order of the enclosing recorded call expression
104
+ public CallInfo nestedIn; // that enclosing site (re-linked when the file order is fixed)
105
+ public String scope = "inline"; // inline | callback | nested | module
106
+ public boolean awaited = false; // Java has no await — contract parity only
107
+ public String defer; // null = no evidence of detachment
108
+ public String deferDetail; // scheduler symbol ("Executors.submit", "@PostConstruct")
109
+ public boolean conc = false;
110
+ public boolean repeat = false;
111
+ public boolean maybe = false;
112
+ public boolean earlyExit = false; // Java has no `?` — contract parity only
82
113
  }
83
114
 
84
115
  public static final class MethodInfo {
@@ -129,6 +160,9 @@ public final class Parser {
129
160
  public List<TypeInfo> types = new ArrayList<>();
130
161
  public Set<String> usedTypeNames = new LinkedHashSet<>();
131
162
  public List<String> errors = new ArrayList<>();
163
+ /** Calls in field initializers + static/instance { } blocks. These run
164
+ * at class-init time and are owned by the FILE node (module scope). */
165
+ public List<CallInfo> fileCalls = new ArrayList<>();
132
166
 
133
167
  /** Top-level + nested types flattened (nested get dotted names). */
134
168
  public List<TypeInfo> allTypes() {
@@ -164,12 +198,29 @@ public final class Parser {
164
198
  for (TypeDeclaration<?> td : cu.getTypes()) {
165
199
  pf.types.add(extractType(td, pf, null));
166
200
  }
201
+ // Field initializers and { } blocks are collected per member, so a
202
+ // file's module-scope sites arrive in member order, not source
203
+ // order — fix that up into ONE 1..N sequence for the FILE node.
204
+ orderFileCalls(pf);
167
205
  } catch (Exception e) {
168
206
  pf.errors.add(e.getMessage() == null ? e.toString() : e.getMessage());
169
207
  }
170
208
  return pf;
171
209
  }
172
210
 
211
+ /** Module-scope sites (FILE node): source order, contiguous ordinals, re-linked nesting. */
212
+ private static void orderFileCalls(ParsedFile pf) {
213
+ pf.fileCalls.sort(Comparator.comparingInt((CallInfo c) -> c.line)
214
+ .thenComparingInt(c -> c.col));
215
+ for (int i = 0; i < pf.fileCalls.size(); i++) {
216
+ CallInfo ci = pf.fileCalls.get(i);
217
+ ci.order = i + 1;
218
+ // an enclosing site always starts before its argument — hence
219
+ // already renumbered here, so `nested` stays a valid, smaller order.
220
+ ci.nested = ci.nestedIn == null ? null : ci.nestedIn.order;
221
+ }
222
+ }
223
+
173
224
  private static TypeInfo extractType(TypeDeclaration<?> td, ParsedFile pf, String outerDotted) {
174
225
  TypeInfo t = new TypeInfo();
175
226
  String simple = td.getNameAsString();
@@ -232,6 +283,9 @@ public final class Parser {
232
283
  f.line = lineOf(fd, true);
233
284
  f.annotations.addAll(annotationNames(fd.getAnnotations()));
234
285
  t.fields.add(f);
286
+ // Field initializers run at class-init time — module scope.
287
+ v.getInitializer().ifPresent(init ->
288
+ pf.fileCalls.addAll(collectCalls(init, null, true)));
235
289
  }
236
290
  }
237
291
  }
@@ -243,6 +297,9 @@ public final class Parser {
243
297
  t.methods.add(extractMethod(md, pf, t.isInterface));
244
298
  } else if (member instanceof ConstructorDeclaration cd) {
245
299
  t.methods.add(extractConstructor(cd, pf));
300
+ } else if (member instanceof InitializerDeclaration id) {
301
+ // static { } / instance { } blocks — module-scope call sites.
302
+ pf.fileCalls.addAll(collectCalls(id.getBody(), null, true));
246
303
  }
247
304
  }
248
305
  }
@@ -297,15 +354,30 @@ public final class Parser {
297
354
  * lambdas attributed to another owner (same guard as Python's
298
355
  * NESTED_SCOPES).
299
356
  */
300
- private static List<CallInfo> collectCalls(BlockStmt body, MethodInfo owner) {
357
+ private static List<CallInfo> collectCalls(Node body, MethodInfo owner) {
358
+ return collectCalls(body, owner, false);
359
+ }
360
+
361
+ /**
362
+ * @param moduleScope when true, skip calls nested in a lambda / anonymous
363
+ * class method inside the node — those execute later, not at init time.
364
+ */
365
+ private static List<CallInfo> collectCalls(Node body, MethodInfo owner, boolean moduleScope) {
301
366
  List<CallInfo> calls = new ArrayList<>();
302
367
  if (body == null) {
303
368
  return calls;
304
369
  }
370
+ // pre-order walk, so an enclosing call is always numbered before the
371
+ // calls nested inside its arguments (nested === its order).
372
+ Map<MethodCallExpr, CallInfo> recorded = new IdentityHashMap<>();
305
373
  body.walk(MethodCallExpr.class, call -> {
374
+ if (moduleScope && isNestedScope(call)) {
375
+ return;
376
+ }
306
377
  CallInfo ci = new CallInfo();
307
378
  ci.methodName = call.getNameAsString();
308
379
  ci.line = lineOf(call, true);
380
+ ci.col = columnOf(call);
309
381
  Expression scope = call.getScope().orElse(null);
310
382
  if (scope != null) {
311
383
  ci.receiverName = rootName(scope);
@@ -325,11 +397,358 @@ public final class Parser {
325
397
  } catch (Throwable ignored) {
326
398
  // unresolved (external lib, local variable, dynamic) — fallback ladder
327
399
  }
400
+ ci.order = calls.size() + 1;
401
+ ci.nestedIn = enclosingRecordedCall(call, recorded);
402
+ ci.nested = ci.nestedIn == null ? null : ci.nestedIn.order;
403
+ ci.scope = moduleScope ? "module" : scopeOf(call, body);
404
+ annotateCallFlags(call, ci, body, owner);
405
+ recorded.put(call, ci);
328
406
  calls.add(ci);
329
407
  });
330
408
  return calls;
331
409
  }
332
410
 
411
+ // ── callOrder/v1 — placement + detachment flags ────────────────────
412
+ // Every annotation below is derived from AST ancestry only: no call is
413
+ // added or removed, so the recorded set stays byte-identical.
414
+
415
+ private static final Set<String> TIMER_TYPES =
416
+ Set.of("Timer", "ScheduledExecutorService", "ScheduledThreadPoolExecutor");
417
+ private static final Set<String> TIMER_MEMBERS =
418
+ Set.of("schedule", "scheduleAtFixedRate", "scheduleWithFixedDelay");
419
+ private static final Set<String> PROMISE_MEMBERS = Set.of(
420
+ "supplyAsync", "runAsync", "thenApply", "thenApplyAsync", "thenAccept",
421
+ "thenAcceptAsync", "thenRun", "thenRunAsync", "thenCompose",
422
+ "thenComposeAsync", "thenCombine", "thenCombineAsync", "applyToEither",
423
+ "applyToEitherAsync", "acceptEither", "acceptEitherAsync", "handle",
424
+ "handleAsync", "whenComplete", "whenCompleteAsync", "completeAsync",
425
+ "exceptionallyAsync");
426
+ private static final Set<String> CONC_MEMBERS = Set.of("submit", "execute", "invokeAll", "invokeAny");
427
+ private static final Set<String> CONC_TYPES = Set.of(
428
+ "Thread", "ForkJoinPool", "ForkJoinTask", "CompletableFuture", "ExecutorService",
429
+ "ScheduledExecutorService", "Executor");
430
+ /** Synchronous iteration combinators: their callback runs 0..N times. */
431
+ private static final Set<String> ITERATION_MEMBERS = Set.of(
432
+ "forEach", "forEachOrdered", "map", "mapToInt", "mapToLong", "mapToDouble",
433
+ "flatMap", "filter", "reduce", "peek", "anyMatch", "allMatch", "noneMatch",
434
+ "sorted", "removeIf");
435
+
436
+ /**
437
+ * The scheduler a call runs on, derived from the expression that received
438
+ * the closure / from the detachment API itself. `defer` may be null when
439
+ * only `conc` / `repeat` are known (ExecutorService.submit(f), stream
440
+ * forEach) — same convention as the TypeScript annotator.
441
+ */
442
+ private record Scheduler(String defer, String detail, boolean conc, boolean repeat) {}
443
+
444
+ private static void annotateCallFlags(MethodCallExpr call, CallInfo ci, Node body, MethodInfo owner) {
445
+ Node frame = nearestFunctionLike(call, body);
446
+ String defer = null;
447
+ String detail = null;
448
+ boolean conc = false;
449
+ boolean repeat = false;
450
+
451
+ // 1. Closure frame: a callback runs on whatever received it.
452
+ if (frame != null) {
453
+ Scheduler s = schedulerOfReceiver(frame, body);
454
+ if (s != null) {
455
+ defer = s.defer();
456
+ detail = s.detail();
457
+ conc = s.conc();
458
+ repeat = s.repeat();
459
+ }
460
+ }
461
+
462
+ // 2. The call site itself may be the detachment API
463
+ // (CompletableFuture.thenApply, timer.schedule, executor.submit, …).
464
+ Scheduler self = schedulerOfCall(call, ci);
465
+ if (self != null) {
466
+ if (defer == null) {
467
+ defer = self.defer();
468
+ detail = self.detail();
469
+ } else if (detail == null) {
470
+ detail = self.detail();
471
+ }
472
+ conc = conc || self.conc();
473
+ repeat = repeat || self.repeat();
474
+ }
475
+
476
+ // 3. Branch / loop / catch ancestry, bounded by the enclosing frame
477
+ // (an outer `if` decides whether the closure runs at all, not the
478
+ // calls inside it — those are the closure's own frame).
479
+ Node boundary = frame != null ? frame : body;
480
+ boolean maybe = false;
481
+ Node child = call;
482
+ Node parent = call.getParentNode().orElse(null);
483
+ while (parent != null && parent != boundary) {
484
+ if (parent instanceof ForStmt || parent instanceof ForEachStmt
485
+ || parent instanceof WhileStmt || parent instanceof DoStmt) {
486
+ repeat = true;
487
+ }
488
+ if (parent instanceof IfStmt ifs) {
489
+ boolean inBranch = child == ifs.getThenStmt()
490
+ || (ifs.getElseStmt().isPresent() && ifs.getElseStmt().get() == child);
491
+ if (inBranch) {
492
+ maybe = true;
493
+ }
494
+ }
495
+ if (parent instanceof ConditionalExpr cond) {
496
+ if (child == cond.getThenExpr() || child == cond.getElseExpr()) {
497
+ maybe = true;
498
+ }
499
+ }
500
+ if (parent instanceof SwitchEntry || parent instanceof CatchClause) {
501
+ maybe = true;
502
+ }
503
+ if (parent instanceof TryStmt ts && ts.getFinallyBlock().isPresent()
504
+ && ts.getFinallyBlock().get() == child) {
505
+ if (defer == null) {
506
+ defer = "onReturn";
507
+ }
508
+ }
509
+ child = parent;
510
+ parent = parent.getParentNode().orElse(null);
511
+ }
512
+
513
+ // 4. Commit-phase lifecycle method: the whole body runs post-render.
514
+ if (defer == null && owner != null && owner.annotations.contains("PostConstruct")) {
515
+ defer = "postRender";
516
+ detail = "@PostConstruct";
517
+ }
518
+
519
+ ci.defer = defer;
520
+ ci.deferDetail = detail;
521
+ ci.conc = conc;
522
+ ci.repeat = repeat;
523
+ ci.maybe = maybe;
524
+ }
525
+
526
+ /**
527
+ * Scheduler of a closure's receiver: `executor.submit(…)`, `new Thread(…)`,
528
+ * `items.forEach(…)`, `someLib.runLater(fn)` → unknown. Null when the
529
+ * closure is not passed to a call/creation or cannot be classified.
530
+ */
531
+ private static Scheduler schedulerOfReceiver(Node frame, Node body) {
532
+ Node receiver;
533
+ if (frame instanceof LambdaExpr lambda) {
534
+ receiver = detachingReceiver(lambda, body);
535
+ } else {
536
+ ObjectCreationExpr anonymous = anonymousCreationOf(frame, body);
537
+ receiver = anonymous == null ? null : detachingReceiver(anonymous, body);
538
+ }
539
+ if (receiver == null) {
540
+ return null;
541
+ }
542
+ if (receiver instanceof ObjectCreationExpr creation) {
543
+ String type = simpleTypeName(creation.getTypeAsString());
544
+ String display = "new " + type;
545
+ if (TIMER_TYPES.contains(type)) {
546
+ return new Scheduler("timer", display, false, false);
547
+ }
548
+ if (CONC_TYPES.contains(type)) {
549
+ return new Scheduler(null, display, true, false);
550
+ }
551
+ return new Scheduler("unknown", display, false, false);
552
+ }
553
+ MethodCallExpr mc = (MethodCallExpr) receiver;
554
+ String display = displayOf(mc);
555
+ String member = mc.getNameAsString();
556
+ String root = scopeRoot(mc);
557
+ if (isCompletionStage(root, member)) {
558
+ return new Scheduler("promise", display, isAsyncCompletion(member), false);
559
+ }
560
+ if (isTimer(root, member)) {
561
+ return new Scheduler("timer", display, false, false);
562
+ }
563
+ if (isConcurrentDispatch(root, member, display)) {
564
+ return new Scheduler(null, display, true, false);
565
+ }
566
+ if (ITERATION_MEMBERS.contains(member)) {
567
+ return new Scheduler(null, display, false, true);
568
+ }
569
+ return new Scheduler("unknown", display, false, false);
570
+ }
571
+
572
+ /** Scheduler signalled by the call site itself; null when it is no detachment API. */
573
+ private static Scheduler schedulerOfCall(MethodCallExpr call, CallInfo ci) {
574
+ String member = call.getNameAsString();
575
+ String root = scopeRoot(call);
576
+ String declaring = simpleTypeName(declaringClassOf(ci.resolvedTarget));
577
+ boolean known = false;
578
+ String defer = null;
579
+ boolean conc = false;
580
+ if (isCompletionStage(root, member) || "CompletableFuture".equals(declaring)) {
581
+ defer = "promise";
582
+ conc = isAsyncCompletion(member);
583
+ known = true;
584
+ } else if (isTimer(root, member) || TIMER_TYPES.contains(declaring)) {
585
+ defer = "timer";
586
+ known = true;
587
+ } else if (isConcurrentDispatch(root, member, ci.name) || CONC_TYPES.contains(declaring)) {
588
+ conc = true;
589
+ known = true;
590
+ }
591
+ if (!known) {
592
+ return null;
593
+ }
594
+ return new Scheduler(defer, ci.name, conc, false);
595
+ }
596
+
597
+ private static boolean isCompletionStage(String root, String member) {
598
+ return "CompletableFuture".equals(root) || PROMISE_MEMBERS.contains(member);
599
+ }
600
+
601
+ private static boolean isAsyncCompletion(String member) {
602
+ return member.endsWith("Async");
603
+ }
604
+
605
+ private static boolean isTimer(String root, String member) {
606
+ return TIMER_MEMBERS.contains(member) || TIMER_TYPES.contains(root);
607
+ }
608
+
609
+ private static boolean isConcurrentDispatch(String root, String member, String display) {
610
+ return CONC_MEMBERS.contains(member) || CONC_TYPES.contains(root)
611
+ || display.contains("parallelStream");
612
+ }
613
+
614
+ /** Nearest enclosing function-like node that is not the owner itself. */
615
+ private static Node nearestFunctionLike(Node node, Node boundary) {
616
+ Node parent = node.getParentNode().orElse(null);
617
+ while (parent != null && parent != boundary) {
618
+ if (parent instanceof LambdaExpr || parent instanceof MethodDeclaration
619
+ || parent instanceof ConstructorDeclaration) {
620
+ return parent;
621
+ }
622
+ parent = parent.getParentNode().orElse(null);
623
+ }
624
+ return null;
625
+ }
626
+
627
+ /** Anonymous class creation expression whose body contains `frame` (or null). */
628
+ private static ObjectCreationExpr anonymousCreationOf(Node frame, Node boundary) {
629
+ Node cur = frame.getParentNode().orElse(null);
630
+ while (cur != null && cur != boundary) {
631
+ if (cur instanceof ObjectCreationExpr oce && oce.getAnonymousClassBody().isPresent()) {
632
+ return oce;
633
+ }
634
+ cur = cur.getParentNode().orElse(null);
635
+ }
636
+ return null;
637
+ }
638
+
639
+ /**
640
+ * The expression that RECEIVES `fn` as an argument — the escape point of a
641
+ * closure (`executor.submit(() -> …)`, `new Thread(runnable)`, `register(fn)`).
642
+ * Null when `fn` is not passed to a call/creation (e.g. a lambda stored in a
643
+ * local variable), i.e. it stays in the owner's frame.
644
+ */
645
+ private static Node detachingReceiver(Node fn, Node boundary) {
646
+ Node child = fn;
647
+ Node parent = fn.getParentNode().orElse(null);
648
+ while (parent != null && parent != boundary) {
649
+ if (parent instanceof MethodCallExpr mc && containsIdentity(mc.getArguments(), child)) {
650
+ return mc;
651
+ }
652
+ if (parent instanceof ObjectCreationExpr oce) {
653
+ if (containsIdentity(oce.getArguments(), child)) {
654
+ return oce;
655
+ }
656
+ if (oce.getAnonymousClassBody().isPresent()
657
+ && containsIdentity(oce.getAnonymousClassBody().get(), child)) {
658
+ return oce;
659
+ }
660
+ }
661
+ if (parent instanceof EnclosedExpr || parent instanceof CastExpr) {
662
+ child = parent;
663
+ parent = parent.getParentNode().orElse(null);
664
+ continue;
665
+ }
666
+ return null;
667
+ }
668
+ return null;
669
+ }
670
+
671
+ /** Execution region of a call site (docs/CALL_ORDER.md §4.3). */
672
+ private static String scopeOf(MethodCallExpr call, Node body) {
673
+ Node frame = nearestFunctionLike(call, body);
674
+ if (frame == null) {
675
+ return "inline";
676
+ }
677
+ Scheduler detached = schedulerOfReceiver(frame, body);
678
+ if (detached != null) {
679
+ return "callback";
680
+ }
681
+ // A lambda or class body that never leaves the owner is a nested
682
+ // declaration the owner runs itself.
683
+ return "nested";
684
+ }
685
+
686
+ /** Nearest enclosing recorded call site of the same owner: bar.nested === foo.order. */
687
+ private static CallInfo enclosingRecordedCall(MethodCallExpr call, Map<MethodCallExpr, CallInfo> recorded) {
688
+ Node parent = call.getParentNode().orElse(null);
689
+ while (parent != null) {
690
+ if (parent instanceof MethodCallExpr mc) {
691
+ CallInfo enclosing = recorded.get(mc);
692
+ if (enclosing != null) {
693
+ return enclosing;
694
+ }
695
+ }
696
+ parent = parent.getParentNode().orElse(null);
697
+ }
698
+ return null;
699
+ }
700
+
701
+ private static boolean containsIdentity(List<? extends Node> nodes, Node target) {
702
+ for (Node n : nodes) {
703
+ if (n == target) {
704
+ return true;
705
+ }
706
+ }
707
+ return false;
708
+ }
709
+
710
+ /** Display string of a call as written: executor.submit, CompletableFuture.supplyAsync. */
711
+ private static String displayOf(MethodCallExpr call) {
712
+ Expression scope = call.getScope().orElse(null);
713
+ return (scope == null ? "" : scopeName(scope) + ".") + call.getNameAsString();
714
+ }
715
+
716
+ /** Root identifier of a call's receiver ("executor" for executor.submit). */
717
+ private static String scopeRoot(MethodCallExpr call) {
718
+ Expression scope = call.getScope().orElse(null);
719
+ if (scope == null) {
720
+ return "";
721
+ }
722
+ if (scope instanceof NameExpr n) {
723
+ return n.getNameAsString();
724
+ }
725
+ String root = rootName(scope);
726
+ return root == null ? "" : root;
727
+ }
728
+
729
+ /** "java.util.concurrent.CompletableFuture.thenApply" → "java.util.concurrent.CompletableFuture". */
730
+ private static String declaringClassOf(String resolvedTarget) {
731
+ if (resolvedTarget == null) {
732
+ return "";
733
+ }
734
+ int dot = resolvedTarget.lastIndexOf('.');
735
+ return dot < 0 ? resolvedTarget : resolvedTarget.substring(0, dot);
736
+ }
737
+
738
+ private static int columnOf(Node n) {
739
+ return n.getRange().map(r -> Math.max(0, r.begin.column - 1)).orElse(0);
740
+ }
741
+
742
+ /**
743
+ * True when the call executes inside a lambda or an anonymous/local class
744
+ * method — i.e. not at the initializer's own init time.
745
+ */
746
+ private static boolean isNestedScope(MethodCallExpr call) {
747
+ return call.findAncestor(LambdaExpr.class).isPresent()
748
+ || call.findAncestor(MethodDeclaration.class).isPresent()
749
+ || call.findAncestor(ConstructorDeclaration.class).isPresent();
750
+ }
751
+
333
752
  /**
334
753
  * Cheap argument type tag in the same vocabulary as declared param types:
335
754
  * literals map directly, anything else goes through the symbol solver
@@ -42,7 +42,8 @@ public final class Calls {
42
42
  String relPath = (String) methodNode.get("filePath");
43
43
  String parentClass = (String) metadata.get("parentClass");
44
44
  String pkg = packageOf(methodNodeId, relPath);
45
- List<String> resolvedCalls = new ArrayList<>();
45
+ Map<String, Map<String, Object>> resolvedRecords = new LinkedHashMap<>();
46
+ Map<String, List<Integer>> ordersByTarget = new LinkedHashMap<>();
46
47
  List<Map<String, String>> resolvedDetails = new ArrayList<>();
47
48
 
48
49
  for (Parser.CallInfo ci : entry.getValue()) {
@@ -53,7 +54,10 @@ public final class Calls {
53
54
  edgeMeta.put("matchedBy", pick.matchedBy);
54
55
  Map<String, Object> edge = Contract.edge("CALLS", methodNodeId, pick.id, edgeMeta);
55
56
  edgesOut.add(edge);
56
- resolvedCalls.add(pick.id);
57
+ if (ci.order > 0) {
58
+ ordersByTarget.computeIfAbsent(pick.id, k -> new ArrayList<>()).add(ci.order);
59
+ }
60
+ resolvedRecords.computeIfAbsent(pick.id, k -> resolvedRecord(ci, lookup, pick.id));
57
61
  // details for OrmEdges (repo R/W classification)
58
62
  Map<String, String> detail = new LinkedHashMap<>();
59
63
  detail.put("target", pick.id);
@@ -62,14 +66,110 @@ public final class Calls {
62
66
  resolvedDetails.add(detail);
63
67
  }
64
68
  }
65
- if (!resolvedCalls.isEmpty()) {
66
- java.util.Collections.sort(resolvedCalls);
67
- metadata.put("resolvedCalls", resolvedCalls);
69
+ if (!resolvedRecords.isEmpty()) {
70
+ attachRepeatOrders(resolvedRecords, ordersByTarget);
71
+ metadata.put("resolvedCalls", new ArrayList<>(resolvedRecords.values()));
72
+ metadata.put("resolvedCallDetails", resolvedDetails);
73
+ }
74
+ }
75
+
76
+ // ── module scope: field initializers + static/instance blocks ──
77
+ // Owned by the FILE node. Same ladder + symbol maps, so the
78
+ // third-party gate (Imports' allowed prefixes) applies identically.
79
+ for (Map.Entry<String, List<Parser.CallInfo>> entry : lookup.fileCallFacts.entrySet()) {
80
+ String callerId = entry.getKey();
81
+ Map<String, Object> caller = lookup.nodeById.get(callerId);
82
+ if (caller == null) {
83
+ continue;
84
+ }
85
+ String relPath = (String) caller.get("filePath");
86
+ String pkg = packageOf(callerId, relPath);
87
+ Map<String, Map<String, Object>> resolvedRecords = new LinkedHashMap<>();
88
+ Map<String, List<Integer>> ordersByTarget = new LinkedHashMap<>();
89
+ List<Map<String, String>> resolvedDetails = new ArrayList<>();
90
+
91
+ for (Parser.CallInfo ci : entry.getValue()) {
92
+ // parentClass null is safe: sameClassMethod builds a
93
+ // "file::x::null" key, which has no candidates.
94
+ Overloads.Pick pick = resolveOne(ci, lookup, relPath, null, pkg, callerId);
95
+ if (pick == null) {
96
+ continue;
97
+ }
98
+ Map<String, Object> edgeMeta = new LinkedHashMap<>();
99
+ edgeMeta.put("line", ci.line);
100
+ edgeMeta.put("matchedBy", pick.matchedBy);
101
+ edgeMeta.put("scope", "module");
102
+ edgesOut.add(Contract.edge("CALLS", callerId, pick.id, edgeMeta));
103
+ if (ci.order > 0) {
104
+ ordersByTarget.computeIfAbsent(pick.id, k -> new ArrayList<>()).add(ci.order);
105
+ }
106
+ resolvedRecords.computeIfAbsent(pick.id, k -> resolvedRecord(ci, lookup, pick.id));
107
+
108
+ Map<String, String> detail = new LinkedHashMap<>();
109
+ detail.put("target", pick.id);
110
+ detail.put("method", ci.methodName);
111
+ detail.put("class", classNodeOf(pick.id));
112
+ resolvedDetails.add(detail);
113
+ }
114
+
115
+ if (!resolvedRecords.isEmpty()) {
116
+ attachRepeatOrders(resolvedRecords, ordersByTarget);
117
+ @SuppressWarnings("unchecked")
118
+ Map<String, Object> metadata = (Map<String, Object>) caller.get("metadata");
119
+ metadata.put("resolvedCalls", new ArrayList<>(resolvedRecords.values()));
68
120
  metadata.put("resolvedCallDetails", resolvedDetails);
69
121
  }
70
122
  }
71
123
  }
72
124
 
125
+ /** One metadata.resolvedCalls entry, shaped exactly like ResolvedCall in
126
+ * src/types.ts. Node metadata only: nothing here depends on edge metadata. */
127
+ private static Map<String, Object> resolvedRecord(Parser.CallInfo ci, LookupMaps lookup, String targetId) {
128
+ Map<String, Object> node = lookup.nodeById.get(targetId);
129
+ String declared = node != null ? (String) node.get("name") : null;
130
+ Map<String, Object> rec = new LinkedHashMap<>();
131
+ rec.put("name", declared != null ? declared : ci.methodName);
132
+ rec.put("nodeId", targetId);
133
+ if (ci.order > 0) {
134
+ rec.put("firstOrder", ci.order);
135
+ rec.put("line", ci.line);
136
+ }
137
+ if (ci.defer != null) {
138
+ rec.put("defer", ci.defer);
139
+ }
140
+ if (ci.deferDetail != null) {
141
+ rec.put("deferDetail", ci.deferDetail);
142
+ }
143
+ if (ci.awaited) {
144
+ rec.put("awaited", true);
145
+ }
146
+ if (ci.conc) {
147
+ rec.put("conc", true);
148
+ }
149
+ if (ci.repeat) {
150
+ rec.put("repeat", true);
151
+ }
152
+ if (ci.maybe) {
153
+ rec.put("maybe", true);
154
+ }
155
+ return rec;
156
+ }
157
+
158
+ /** orders[] only when a target was reached more than once. */
159
+ private static void attachRepeatOrders(Map<String, Map<String, Object>> records,
160
+ Map<String, List<Integer>> orders) {
161
+ for (Map.Entry<String, List<Integer>> e : orders.entrySet()) {
162
+ Map<String, Object> rec = records.get(e.getKey());
163
+ if (rec == null) {
164
+ continue;
165
+ }
166
+ List<Integer> unique = e.getValue().stream().distinct().sorted().toList();
167
+ if (unique.size() > 1) {
168
+ rec.put("orders", unique);
169
+ }
170
+ }
171
+ }
172
+
73
173
  private static Overloads.Pick resolveOne(Parser.CallInfo ci, LookupMaps lookup,
74
174
  String relPath, String parentClass,
75
175
  String pkg, String methodNodeId) {