@mrclrchtr/supi-code-intelligence 11.0.1 → 11.2.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 (200) hide show
  1. package/README.md +68 -6
  2. package/gradle-helper/README.md +48 -0
  3. package/gradle-helper/build.sh +73 -0
  4. package/gradle-helper/lib/gradle-tooling-api-9.4.0.jar +0 -0
  5. package/gradle-helper/lib/slf4j-api-2.0.17.jar +0 -0
  6. package/gradle-helper/lib/slf4j-simple-2.0.17.jar +0 -0
  7. package/gradle-helper/lib/supi-gradle-observation.jar +0 -0
  8. package/gradle-helper/licenses/DEPENDENCIES.md +11 -0
  9. package/gradle-helper/licenses/LICENSE-gradle-tooling-api.txt +422 -0
  10. package/gradle-helper/licenses/LICENSE-slf4j.txt +21 -0
  11. package/gradle-helper/licenses/NOTICE-gradle.txt +21 -0
  12. package/gradle-helper/src/GradleObservation.java +597 -0
  13. package/node_modules/@mrclrchtr/supi-code-runtime/package.json +1 -1
  14. package/node_modules/@mrclrchtr/supi-core/package.json +1 -1
  15. package/node_modules/@mrclrchtr/supi-core/src/debug-registry.ts +41 -1
  16. package/node_modules/@mrclrchtr/supi-lsp/README.md +43 -6
  17. package/node_modules/@mrclrchtr/supi-lsp/node_modules/@mrclrchtr/supi-code-runtime/package.json +1 -1
  18. package/node_modules/@mrclrchtr/supi-lsp/node_modules/@mrclrchtr/supi-core/package.json +1 -1
  19. package/node_modules/@mrclrchtr/supi-lsp/node_modules/@mrclrchtr/supi-core/src/debug-registry.ts +41 -1
  20. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/LICENSE +15 -0
  21. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/README.md +680 -0
  22. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/bin/semver.js +195 -0
  23. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/classes/comparator.js +143 -0
  24. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/classes/index.js +7 -0
  25. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/classes/range.js +577 -0
  26. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/classes/semver.js +350 -0
  27. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/functions/clean.js +8 -0
  28. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/functions/cmp.js +54 -0
  29. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/functions/coerce.js +62 -0
  30. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/functions/compare-build.js +9 -0
  31. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/functions/compare-loose.js +5 -0
  32. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/functions/compare.js +7 -0
  33. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/functions/diff.js +60 -0
  34. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/functions/eq.js +5 -0
  35. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/functions/gt.js +5 -0
  36. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/functions/gte.js +5 -0
  37. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/functions/inc.js +21 -0
  38. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/functions/lt.js +5 -0
  39. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/functions/lte.js +5 -0
  40. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/functions/major.js +5 -0
  41. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/functions/minor.js +5 -0
  42. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/functions/neq.js +5 -0
  43. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/functions/parse.js +18 -0
  44. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/functions/patch.js +5 -0
  45. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/functions/prerelease.js +8 -0
  46. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/functions/rcompare.js +5 -0
  47. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/functions/rsort.js +5 -0
  48. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/functions/satisfies.js +12 -0
  49. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/functions/sort.js +5 -0
  50. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/functions/truncate.js +48 -0
  51. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/functions/valid.js +8 -0
  52. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/index.js +93 -0
  53. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/internal/constants.js +37 -0
  54. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/internal/debug.js +11 -0
  55. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/internal/identifiers.js +29 -0
  56. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/internal/lrucache.js +42 -0
  57. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/internal/parse-options.js +17 -0
  58. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/internal/re.js +223 -0
  59. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/package.json +78 -0
  60. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/preload.js +4 -0
  61. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/range.bnf +17 -0
  62. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/ranges/gtr.js +6 -0
  63. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/ranges/intersects.js +9 -0
  64. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/ranges/ltr.js +6 -0
  65. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/ranges/max-satisfying.js +27 -0
  66. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/ranges/min-satisfying.js +26 -0
  67. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/ranges/min-version.js +63 -0
  68. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/ranges/outside.js +82 -0
  69. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/ranges/simplify.js +49 -0
  70. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/ranges/subset.js +249 -0
  71. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/ranges/to-comparators.js +10 -0
  72. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/ranges/valid.js +13 -0
  73. package/node_modules/@mrclrchtr/supi-lsp/node_modules/vscode-jsonrpc/README.md +5 -0
  74. package/node_modules/@mrclrchtr/supi-lsp/node_modules/vscode-jsonrpc/lib/browser/ril.js +8 -0
  75. package/node_modules/@mrclrchtr/supi-lsp/node_modules/vscode-jsonrpc/lib/common/connection.js +7 -2
  76. package/node_modules/@mrclrchtr/supi-lsp/node_modules/vscode-jsonrpc/lib/common/ral.d.ts +8 -0
  77. package/node_modules/@mrclrchtr/supi-lsp/node_modules/vscode-jsonrpc/lib/node/main.d.ts +14 -0
  78. package/node_modules/@mrclrchtr/supi-lsp/node_modules/vscode-jsonrpc/lib/node/main.js +6 -4
  79. package/node_modules/@mrclrchtr/supi-lsp/node_modules/vscode-jsonrpc/lib/node/ril.js +5 -1
  80. package/node_modules/@mrclrchtr/supi-lsp/node_modules/vscode-jsonrpc/package.json +4 -2
  81. package/node_modules/@mrclrchtr/supi-lsp/node_modules/vscode-languageserver-protocol/package.json +4 -3
  82. package/node_modules/@mrclrchtr/supi-lsp/node_modules/vscode-languageserver-types/lib/esm/main.js +2 -2
  83. package/node_modules/@mrclrchtr/supi-lsp/node_modules/vscode-languageserver-types/lib/umd/main.js +2 -2
  84. package/node_modules/@mrclrchtr/supi-lsp/node_modules/vscode-languageserver-types/package.json +2 -1
  85. package/node_modules/@mrclrchtr/supi-lsp/package.json +8 -6
  86. package/node_modules/@mrclrchtr/supi-lsp/src/client/client.ts +44 -5
  87. package/node_modules/@mrclrchtr/supi-lsp/src/config/config.ts +53 -2
  88. package/node_modules/@mrclrchtr/supi-lsp/src/config/operation-support.ts +19 -4
  89. package/node_modules/@mrclrchtr/supi-lsp/src/config/server-config.ts +22 -0
  90. package/node_modules/@mrclrchtr/supi-lsp/src/config/typescript-launch.ts +276 -0
  91. package/node_modules/@mrclrchtr/supi-lsp/src/manager/manager-project-info.ts +22 -10
  92. package/node_modules/@mrclrchtr/supi-lsp/src/manager/manager.ts +53 -20
  93. package/node_modules/@mrclrchtr/supi-lsp/src/provider/refactor-planning.ts +4 -3
  94. package/node_modules/@mrclrchtr/supi-lsp/src/session/runtime-controller.ts +2 -2
  95. package/node_modules/@mrclrchtr/supi-lsp/src/session/scanner.ts +27 -15
  96. package/node_modules/@mrclrchtr/supi-tree-sitter/README.md +1 -0
  97. package/node_modules/@mrclrchtr/supi-tree-sitter/node_modules/@mrclrchtr/supi-code-runtime/package.json +1 -1
  98. package/node_modules/@mrclrchtr/supi-tree-sitter/node_modules/@mrclrchtr/supi-core/package.json +1 -1
  99. package/node_modules/@mrclrchtr/supi-tree-sitter/node_modules/@mrclrchtr/supi-core/src/debug-registry.ts +41 -1
  100. package/node_modules/@mrclrchtr/supi-tree-sitter/package.json +5 -3
  101. package/node_modules/@mrclrchtr/supi-tree-sitter/resources/grammars/tsx/LICENSE +21 -0
  102. package/node_modules/@mrclrchtr/supi-tree-sitter/resources/grammars/tsx/tree-sitter-tsx.wasm +0 -0
  103. package/node_modules/@mrclrchtr/supi-tree-sitter/resources/grammars/tsx/tree-sitter-tsx.wasm.json +19 -2
  104. package/node_modules/@mrclrchtr/supi-tree-sitter/resources/grammars/typescript/LICENSE +21 -0
  105. package/node_modules/@mrclrchtr/supi-tree-sitter/resources/grammars/typescript/tree-sitter-typescript.wasm +0 -0
  106. package/node_modules/@mrclrchtr/supi-tree-sitter/resources/grammars/typescript/tree-sitter-typescript.wasm.json +19 -2
  107. package/node_modules/ignore/README.md +36 -10
  108. package/node_modules/ignore/index.d.ts +3 -2
  109. package/node_modules/ignore/index.js +57 -53
  110. package/node_modules/ignore/legacy.js +50 -46
  111. package/node_modules/ignore/package.json +1 -1
  112. package/package.json +11 -6
  113. package/src/analysis/capability/capability-warnings.ts +18 -12
  114. package/src/analysis/gradle/query.ts +273 -0
  115. package/src/analysis/gradle/runtime.ts +173 -0
  116. package/src/analysis/gradle/types.ts +65 -0
  117. package/src/analysis/health/check.ts +115 -0
  118. package/src/analysis/health/diagnostics.ts +3 -0
  119. package/src/analysis/health/file-scope.ts +2 -3
  120. package/src/analysis/health/semantic-state.ts +38 -0
  121. package/src/analysis/health/server-status.ts +8 -0
  122. package/src/analysis/health/signals.ts +2 -0
  123. package/src/analysis/health/status.ts +82 -0
  124. package/src/analysis/read-next.ts +1 -4
  125. package/src/analysis/refactor/apply.ts +19 -10
  126. package/src/analysis/refactor/mutation-authority.ts +4 -1
  127. package/src/analysis/search/ast-scan.ts +143 -18
  128. package/src/analysis/workspace/safe-path.ts +14 -2
  129. package/src/analysis/workspace/types.ts +2 -0
  130. package/src/extension.ts +10 -1
  131. package/src/gradle/consent.ts +279 -0
  132. package/src/headless.ts +3 -0
  133. package/src/overview/collect.ts +11 -3
  134. package/src/session/health-types.ts +7 -0
  135. package/src/session/health-workflow.ts +8 -43
  136. package/src/session/orientation/collect.ts +138 -12
  137. package/src/session/orientation/context-facts.ts +27 -2
  138. package/src/session/orientation/context-sections.ts +47 -0
  139. package/src/session/orientation/facts.ts +124 -0
  140. package/src/session/orientation/gather.ts +98 -11
  141. package/src/session/orientation/workspace-context.ts +59 -28
  142. package/src/session/orientation-types.ts +16 -1
  143. package/src/session/orientation-workflow.ts +5 -2
  144. package/src/session/refactor-plans.ts +5 -2
  145. package/src/session/refactor-workflow.ts +4 -3
  146. package/src/session/session.ts +8 -0
  147. package/src/tool/code_find/execute.ts +1 -1
  148. package/src/tool/code_find/guidance.ts +1 -1
  149. package/src/tool/code_find/markdown.ts +24 -17
  150. package/src/tool/code_find/result.ts +18 -5
  151. package/src/tool/code_find/spec.ts +2 -0
  152. package/src/tool/code_find/structured.ts +161 -0
  153. package/src/tool/code_graph/result.ts +32 -15
  154. package/src/tool/code_graph/spec.ts +2 -0
  155. package/src/tool/code_graph/structured.ts +142 -0
  156. package/src/tool/code_health/markdown.ts +17 -17
  157. package/src/tool/code_health/result.ts +15 -2
  158. package/src/tool/code_health/spec.ts +2 -0
  159. package/src/tool/code_health/structured-schema.ts +250 -0
  160. package/src/tool/code_health/structured.ts +256 -0
  161. package/src/tool/code_inspect/markdown.ts +19 -11
  162. package/src/tool/code_inspect/result.ts +12 -5
  163. package/src/tool/code_inspect/spec.ts +2 -0
  164. package/src/tool/code_inspect/structured.ts +232 -0
  165. package/src/tool/code_orientation/directory-navigation.ts +66 -0
  166. package/src/tool/code_orientation/gradle-sections.ts +68 -0
  167. package/src/tool/code_orientation/markdown.ts +19 -8
  168. package/src/tool/code_orientation/result.ts +95 -11
  169. package/src/tool/code_orientation/spec.ts +2 -0
  170. package/src/tool/code_orientation/structured-schema.ts +607 -0
  171. package/src/tool/code_orientation/structured-workspace.ts +193 -0
  172. package/src/tool/code_orientation/structured.ts +195 -0
  173. package/src/tool/code_orientation/workspace-format.ts +2 -1
  174. package/src/tool/code_orientation/workspace-observations.ts +81 -0
  175. package/src/tool/code_orientation/workspace-output.ts +45 -13
  176. package/src/tool/code_orientation/workspace-projects.ts +180 -75
  177. package/src/tool/code_orientation/workspace-sections.ts +46 -31
  178. package/src/tool/code_refactor_apply/execute.ts +1 -1
  179. package/src/tool/code_refactor_apply/result.ts +13 -5
  180. package/src/tool/code_refactor_apply/spec.ts +2 -0
  181. package/src/tool/code_refactor_apply/structured.ts +49 -0
  182. package/src/tool/code_refactor_plan/result.ts +16 -8
  183. package/src/tool/code_refactor_plan/spec.ts +2 -0
  184. package/src/tool/code_refactor_plan/structured.ts +128 -0
  185. package/src/tool/code_resolve/markdown.ts +4 -10
  186. package/src/tool/code_resolve/result.ts +2 -0
  187. package/src/tool/code_resolve/spec.ts +2 -0
  188. package/src/tool/code_resolve/structured.ts +103 -0
  189. package/src/tool/infra/truncate.ts +12 -4
  190. package/src/tool/register.ts +10 -6
  191. package/src/tool/result/display.ts +17 -0
  192. package/src/tool/result/model-budget.ts +195 -0
  193. package/src/tool/result/structured.ts +62 -0
  194. package/src/tool/result/types.ts +10 -2
  195. package/src/tool/schemas.ts +7 -1
  196. package/src/tool/specs.ts +2 -0
  197. package/src/types/execution.ts +5 -1
  198. package/src/ui/status-command.ts +110 -321
  199. package/src/ui/status-overlay.ts +564 -420
  200. package/src/ui/tui/display.ts +10 -4
@@ -0,0 +1,597 @@
1
+ import org.gradle.tooling.BuildException;
2
+ import org.gradle.tooling.CancellationToken;
3
+ import org.gradle.tooling.CancellationTokenSource;
4
+ import org.gradle.tooling.GradleConnectionException;
5
+ import org.gradle.tooling.GradleConnector;
6
+ import org.gradle.tooling.ModelBuilder;
7
+ import org.gradle.tooling.ProjectConnection;
8
+ import org.gradle.tooling.model.UnsupportedMethodException;
9
+ import org.gradle.tooling.model.build.BuildEnvironment;
10
+ import org.gradle.tooling.model.gradle.BasicGradleProject;
11
+ import org.gradle.tooling.model.gradle.GradleBuild;
12
+
13
+ import java.io.BufferedReader;
14
+ import java.io.ByteArrayInputStream;
15
+ import java.io.File;
16
+ import java.io.IOException;
17
+ import java.io.InputStreamReader;
18
+ import java.io.OutputStream;
19
+ import java.io.PrintStream;
20
+ import java.net.URI;
21
+ import java.nio.charset.StandardCharsets;
22
+ import java.util.ArrayDeque;
23
+ import java.util.ArrayList;
24
+ import java.util.Collections;
25
+ import java.util.Comparator;
26
+ import java.util.Deque;
27
+ import java.util.IdentityHashMap;
28
+ import java.util.LinkedHashSet;
29
+ import java.util.List;
30
+ import java.util.Locale;
31
+ import java.util.Set;
32
+ import java.util.concurrent.CompletableFuture;
33
+ import java.util.concurrent.ExecutionException;
34
+ import java.util.concurrent.ScheduledExecutorService;
35
+ import java.util.concurrent.Executors;
36
+ import java.util.concurrent.TimeUnit;
37
+ import java.util.concurrent.TimeoutException;
38
+ import java.util.concurrent.atomic.AtomicLong;
39
+ import java.util.regex.Matcher;
40
+ import java.util.regex.Pattern;
41
+
42
+ /**
43
+ * Bounded, read-only Gradle build-structure probe for a separately managed JVM process.
44
+ *
45
+ * Arguments: selected-root, runtime-kind (distribution|installation), runtime-value,
46
+ * deadline-epoch-milliseconds. A line containing "cancel" on stdin cancels the current
47
+ * Tooling API request. The helper writes exactly one bounded JSON response to stdout.
48
+ */
49
+ public final class GradleObservation {
50
+ private static final int MAX_PROJECTS = 10_000;
51
+ private static final int MAX_OUTPUT_BYTES = 1_048_576;
52
+ private static final int MAX_LOG_BYTES = 64 * 1024;
53
+ private static final int MAX_FAILURE_REASON_CHARS = 500;
54
+ private static final int MAX_FAILURE_CAUSE_DEPTH = 8;
55
+ private static final Pattern VERSION_PATTERN = Pattern.compile("^(\\d+)(?:\\.|$)");
56
+ private static final PrintStream JSON_OUTPUT = System.out;
57
+ private static final PrintStream ORIGINAL_STDERR = System.err;
58
+ private static final PrintStream LOG_OUTPUT = new PrintStream(
59
+ new CappedLogOutputStream(ORIGINAL_STDERR, MAX_LOG_BYTES), true, StandardCharsets.UTF_8);
60
+
61
+ private GradleObservation() {
62
+ }
63
+
64
+ public static void main(String[] args) {
65
+ System.setOut(LOG_OUTPUT);
66
+ System.setErr(LOG_OUTPUT);
67
+ System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "warn");
68
+
69
+ if (args.length != 4) {
70
+ emitFailure("failed", "Expected selected-root, runtime-kind, runtime-value, and deadline-epoch-milliseconds");
71
+ return;
72
+ }
73
+
74
+ final long deadlineEpochMillis;
75
+ try {
76
+ deadlineEpochMillis = Long.parseLong(args[3]);
77
+ } catch (NumberFormatException error) {
78
+ emitFailure("failed", "Deadline must be an epoch-millisecond integer");
79
+ return;
80
+ }
81
+
82
+ final File selectedRoot;
83
+ try {
84
+ selectedRoot = new File(args[0]).getCanonicalFile();
85
+ } catch (IOException error) {
86
+ emitFailure("unavailable", "Selected root could not be canonicalized");
87
+ return;
88
+ }
89
+ if (!selectedRoot.isDirectory()) {
90
+ emitFailure("unavailable", "Selected root is not a directory");
91
+ return;
92
+ }
93
+ if (!hasBuildDefinition(selectedRoot)) {
94
+ emitFailure("unavailable", "Selected root has no Gradle settings file");
95
+ return;
96
+ }
97
+ if (!"distribution".equals(args[1]) && !"installation".equals(args[1])) {
98
+ emitFailure("failed", "Runtime kind must be distribution or installation");
99
+ return;
100
+ }
101
+
102
+ final CancellationTokenSource tokenSource;
103
+ try {
104
+ tokenSource = GradleConnector.newCancellationTokenSource();
105
+ } catch (Throwable error) {
106
+ emitFailure("failed", safeMessage(error));
107
+ return;
108
+ }
109
+ CancellationState cancellation = new CancellationState(tokenSource);
110
+ startCancellationInput(cancellation);
111
+
112
+ ScheduledExecutorService watchdog = Executors.newSingleThreadScheduledExecutor(task -> {
113
+ Thread thread = new Thread(task, "gradle-observation-deadline");
114
+ thread.setDaemon(true);
115
+ return thread;
116
+ });
117
+ long delayMillis = Math.max(0L, deadlineEpochMillis - System.currentTimeMillis());
118
+ if (delayMillis == 0L) {
119
+ cancellation.request("timeout");
120
+ } else {
121
+ watchdog.schedule(() -> cancellation.request("timeout"), delayMillis, TimeUnit.MILLISECONDS);
122
+ }
123
+
124
+ ObservationState observation = new ObservationState();
125
+ CompletableFuture<String> work = new CompletableFuture<>();
126
+ Thread worker = new Thread(() -> {
127
+ try {
128
+ work.complete(observe(selectedRoot, args[1], args[2], tokenSource.token(), observation));
129
+ } catch (Throwable error) {
130
+ work.completeExceptionally(error);
131
+ }
132
+ }, "gradle-observation-model");
133
+ worker.setDaemon(true);
134
+ if (cancellation.reason() == null) {
135
+ worker.start();
136
+ }
137
+
138
+ String response;
139
+ try {
140
+ long waitMillis = Math.max(1L, deadlineEpochMillis - System.currentTimeMillis());
141
+ CompletableFuture.anyOf(work, cancellation.signal).get(waitMillis, TimeUnit.MILLISECONDS);
142
+ if (cancellation.reason() != null) {
143
+ response = failureJson(cancellation.reason(), cancellation.reason().equals("timeout")
144
+ ? "Gradle observation deadline expired"
145
+ : "Gradle observation was cancelled", observation);
146
+ } else {
147
+ response = work.get();
148
+ }
149
+ } catch (TimeoutException error) {
150
+ cancellation.request("timeout");
151
+ response = failureJson("timeout", "Gradle observation deadline expired", observation);
152
+ } catch (InterruptedException error) {
153
+ Thread.currentThread().interrupt();
154
+ cancellation.request("interrupted");
155
+ response = failureJson("interrupted", "Gradle observation was interrupted", observation);
156
+ } catch (ExecutionException error) {
157
+ if (cancellation.reason() != null) {
158
+ String reason = cancellation.reason();
159
+ response = failureJson(reason, reason.equals("timeout")
160
+ ? "Gradle observation deadline expired"
161
+ : "Gradle observation was cancelled", observation);
162
+ } else {
163
+ response = exceptionJson(error.getCause(), observation);
164
+ }
165
+ } catch (Throwable error) {
166
+ response = exceptionJson(error, observation);
167
+ } finally {
168
+ watchdog.shutdownNow();
169
+ }
170
+
171
+ if (!cancellation.commit()) {
172
+ String reason = cancellation.reason();
173
+ response = failureJson(reason, reason.equals("timeout")
174
+ ? "Gradle observation deadline expired"
175
+ : "Gradle observation was cancelled", observation);
176
+ }
177
+ emit(response);
178
+ if (cancellation.reason() != null) {
179
+ // Tooling API cancellation is best-effort; the parent owns a final process-kill grace period.
180
+ Runtime.getRuntime().halt(0);
181
+ }
182
+ }
183
+
184
+ /** Metadata shared with the deadline thread, recorded at actual collection boundaries. */
185
+ private static final class ObservationState {
186
+ private volatile String stage = "prerequisites";
187
+ private volatile String gradleVersion;
188
+ }
189
+
190
+ private static boolean hasBuildDefinition(File root) {
191
+ return new File(root, "settings.gradle").isFile()
192
+ || new File(root, "settings.gradle.kts").isFile();
193
+ }
194
+
195
+ private static String observe(
196
+ File selectedRoot,
197
+ String runtimeKind,
198
+ String runtimeValue,
199
+ CancellationToken token,
200
+ ObservationState observation) throws Exception {
201
+ GradleConnector connector = GradleConnector.newConnector().forProjectDirectory(selectedRoot);
202
+ if (runtimeKind.equals("distribution")) {
203
+ connector.useDistribution(URI.create(runtimeValue));
204
+ } else {
205
+ File installation = new File(runtimeValue).getCanonicalFile();
206
+ if (!installation.isDirectory()) {
207
+ throw new ObservationFailure("unavailable", "Selected Gradle installation is not a directory");
208
+ }
209
+ connector.useInstallation(installation);
210
+ }
211
+
212
+ File javaHome = new File(System.getProperty("java.home")).getCanonicalFile();
213
+ try (ProjectConnection connection = connector.connect()) {
214
+ ModelBuilder<BuildEnvironment> environmentRequest = configured(
215
+ connection.model(BuildEnvironment.class), token, javaHome);
216
+ checkCancelled(token);
217
+ observation.stage = "query";
218
+ BuildEnvironment environment = environmentRequest.get();
219
+ String gradleVersion = environment.getGradle().getGradleVersion();
220
+ observation.gradleVersion = gradleVersion;
221
+ requireGradle9(gradleVersion);
222
+ checkCancelled(token);
223
+
224
+ // The only project model requested is GradleBuild; no task, dependency, or IDE models are queried.
225
+ ModelBuilder<GradleBuild> buildRequest = configured(
226
+ connection.model(GradleBuild.class), token, javaHome);
227
+ GradleBuild build = buildRequest.get();
228
+ checkCancelled(token);
229
+ return serialize(build, gradleVersion, token);
230
+ } catch (ObservationFailure error) {
231
+ throw error;
232
+ } catch (IOException error) {
233
+ throw new ObservationFailure("unavailable", "Gradle connection could not be closed");
234
+ }
235
+ }
236
+
237
+ private static <T> ModelBuilder<T> configured(
238
+ ModelBuilder<T> builder,
239
+ CancellationToken token,
240
+ File javaHome) {
241
+ return builder.withCancellationToken(token)
242
+ .setJavaHome(javaHome)
243
+ .setStandardOutput(LOG_OUTPUT)
244
+ .setStandardError(LOG_OUTPUT)
245
+ .setStandardInput(new ByteArrayInputStream(new byte[0]))
246
+ .setColorOutput(false)
247
+ .withArguments("--console=plain");
248
+ }
249
+
250
+ private static void requireGradle9(String version) throws ObservationFailure {
251
+ Matcher matcher = VERSION_PATTERN.matcher(version == null ? "" : version);
252
+ if (!matcher.find()) {
253
+ throw new ObservationFailure("unsupported", "Gradle version could not be identified");
254
+ }
255
+ try {
256
+ int major = Integer.parseInt(matcher.group(1));
257
+ if (major != 9) {
258
+ throw new ObservationFailure("unsupported", "Only Gradle 9 is supported");
259
+ }
260
+ } catch (NumberFormatException error) {
261
+ throw new ObservationFailure("unsupported", "Gradle version could not be identified");
262
+ }
263
+ }
264
+
265
+ private static String serialize(GradleBuild build, String gradleVersion, CancellationToken token)
266
+ throws Exception {
267
+ String buildRoot = canonical(build.getBuildIdentifier().getRootDir());
268
+ List<ProjectRecord> projects = new ArrayList<>();
269
+ Deque<BasicGradleProject> pending = new ArrayDeque<>();
270
+ pending.add(build.getRootProject());
271
+ while (!pending.isEmpty()) {
272
+ checkCancelled(token);
273
+ BasicGradleProject project = pending.removeFirst();
274
+ if (projects.size() >= MAX_PROJECTS) {
275
+ throw new ObservationFailure("oversized", "Project count exceeds 10000");
276
+ }
277
+ String parentPath = project.getParent() == null
278
+ ? null
279
+ : project.getParent().getProjectIdentifier().getProjectPath();
280
+ projects.add(new ProjectRecord(
281
+ canonical(project.getProjectIdentifier().getBuildIdentifier().getRootDir()),
282
+ project.getProjectIdentifier().getProjectPath(),
283
+ project.getName(),
284
+ canonical(project.getProjectDirectory()),
285
+ parentPath));
286
+ for (BasicGradleProject child : project.getChildren()) {
287
+ pending.addLast(child);
288
+ }
289
+ }
290
+ projects.sort(Comparator.comparing(ProjectRecord::path));
291
+
292
+ JsonWriter json = new JsonWriter();
293
+ json.raw("{\"status\":\"complete\",\"gradleVersion\":");
294
+ json.string(gradleVersion);
295
+ json.raw(",\"buildRoot\":");
296
+ json.string(buildRoot);
297
+ json.raw(",\"projects\":[");
298
+ for (int index = 0; index < projects.size(); index++) {
299
+ checkCancelled(token);
300
+ if (index > 0) {
301
+ json.raw(",");
302
+ }
303
+ ProjectRecord project = projects.get(index);
304
+ json.raw("{\"buildRoot\":");
305
+ json.string(project.buildRoot());
306
+ json.raw(",\"path\":");
307
+ json.string(project.path());
308
+ json.raw(",\"name\":");
309
+ json.string(project.name());
310
+ json.raw(",\"directory\":");
311
+ json.string(project.directory());
312
+ json.raw(",\"parentPath\":");
313
+ if (project.parentPath() == null) {
314
+ json.raw("null");
315
+ } else {
316
+ json.string(project.parentPath());
317
+ }
318
+ json.raw("}");
319
+ }
320
+ json.raw("],\"includedBuilds\":[");
321
+ int index = 0;
322
+ for (GradleBuild included : build.getIncludedBuilds()) {
323
+ checkCancelled(token);
324
+ if (index++ > 0) {
325
+ json.raw(",");
326
+ }
327
+ json.raw("{\"buildRoot\":");
328
+ json.string(canonical(included.getBuildIdentifier().getRootDir()));
329
+ json.raw("}");
330
+ }
331
+ json.raw("]}");
332
+ return json.toString();
333
+ }
334
+
335
+ private static void checkCancelled(CancellationToken token) throws ObservationFailure {
336
+ if (token.isCancellationRequested()) {
337
+ throw new ObservationFailure("interrupted", "Gradle observation was cancelled");
338
+ }
339
+ }
340
+
341
+ private static String canonical(File file) throws IOException {
342
+ return file.getCanonicalPath();
343
+ }
344
+
345
+ private static void startCancellationInput(CancellationState cancellation) {
346
+ Thread reader = new Thread(() -> {
347
+ try (BufferedReader input = new BufferedReader(
348
+ new InputStreamReader(System.in, StandardCharsets.UTF_8))) {
349
+ String line;
350
+ while ((line = input.readLine()) != null) {
351
+ if (line.trim().equals("cancel")) {
352
+ cancellation.request("interrupted");
353
+ return;
354
+ }
355
+ }
356
+ } catch (IOException ignored) {
357
+ // The parent may close stdin while the observation is in progress.
358
+ }
359
+ }, "gradle-observation-cancellation-input");
360
+ reader.setDaemon(true);
361
+ reader.start();
362
+ }
363
+
364
+ private static String exceptionJson(Throwable error, ObservationState observation) {
365
+ Throwable cause = unwrap(error);
366
+ if (cause instanceof ObservationFailure failure) {
367
+ return failureJson(failure.status, failure.getMessage(), observation);
368
+ }
369
+ if (cause instanceof JsonTooLargeException) {
370
+ return failureJson("oversized", "Observation output exceeds 1 MiB", observation);
371
+ }
372
+ if (cause instanceof UnsupportedMethodException) {
373
+ return failureJson("unsupported", "Gradle does not support the requested model operation", observation);
374
+ }
375
+ if (cause instanceof BuildException) {
376
+ return failureJson("failed", safeMessage(cause), observation);
377
+ }
378
+ if (cause instanceof GradleConnectionException) {
379
+ return failureJson("unavailable", safeMessage(cause), observation);
380
+ }
381
+ if (cause instanceof InterruptedException) {
382
+ return failureJson("interrupted", "Gradle observation was interrupted", observation);
383
+ }
384
+ return failureJson("failed", safeMessage(cause), observation);
385
+ }
386
+
387
+ private static Throwable unwrap(Throwable error) {
388
+ Throwable current = error;
389
+ while ((current instanceof ExecutionException || current instanceof java.util.concurrent.CompletionException)
390
+ && current.getCause() != null) {
391
+ current = current.getCause();
392
+ }
393
+ return current;
394
+ }
395
+
396
+ /** Keep the deepest available cause first so generic Tooling API context cannot crowd it out. */
397
+ private static String safeMessage(Throwable error) {
398
+ Set<Throwable> seen = Collections.newSetFromMap(new IdentityHashMap<>());
399
+ List<String> messages = new ArrayList<>();
400
+ Throwable current = error;
401
+ for (int depth = 0; current != null && depth < MAX_FAILURE_CAUSE_DEPTH && seen.add(current); depth++) {
402
+ String message = current.getMessage();
403
+ if (message == null || message.isBlank()) {
404
+ message = current.getClass().getSimpleName();
405
+ }
406
+ if (message.length() > MAX_FAILURE_REASON_CHARS) {
407
+ message = message.substring(0, MAX_FAILURE_REASON_CHARS - 1) + "…";
408
+ }
409
+ message = message.replaceAll("[\\p{Cc}\\p{Cf}\\p{Z}]", " ").replaceAll("(?U)\\s+", " ").trim();
410
+ if (!message.isEmpty()) messages.add(message);
411
+ current = current.getCause();
412
+ }
413
+ Collections.reverse(messages);
414
+ StringBuilder summary = new StringBuilder();
415
+ for (String message : new LinkedHashSet<>(messages)) {
416
+ String separator = summary.length() == 0 ? "" : "; context: ";
417
+ int remaining = MAX_FAILURE_REASON_CHARS - summary.length() - separator.length();
418
+ if (remaining <= 1) break;
419
+ summary.append(separator);
420
+ if (message.length() > remaining) {
421
+ summary.append(message, 0, remaining - 1).append("…");
422
+ break;
423
+ }
424
+ summary.append(message);
425
+ }
426
+ return summary.length() == 0 ? "Gradle observation failed" : summary.toString();
427
+ }
428
+
429
+ private static String failureJson(String status, String reason) {
430
+ return failureJson(status, reason, null);
431
+ }
432
+
433
+ private static String failureJson(String status, String reason, ObservationState observation) {
434
+ JsonWriter json = new JsonWriter();
435
+ json.raw("{\"status\":");
436
+ json.string(status);
437
+ json.raw(",\"stage\":");
438
+ json.string(observation == null ? "prerequisites" : observation.stage);
439
+ json.raw(",\"reason\":");
440
+ json.string(reason == null ? "Gradle observation failed" : reason);
441
+ if (observation != null && observation.gradleVersion != null) {
442
+ json.raw(",\"gradleVersion\":");
443
+ json.string(observation.gradleVersion);
444
+ }
445
+ json.raw("}");
446
+ return json.toString();
447
+ }
448
+
449
+ private static void emitFailure(String status, String reason) {
450
+ emit(failureJson(status, reason));
451
+ }
452
+
453
+ private static void emit(String response) {
454
+ byte[] bytes = (response + "\n").getBytes(StandardCharsets.UTF_8);
455
+ if (bytes.length > MAX_OUTPUT_BYTES) {
456
+ bytes = (failureJson("oversized", "Observation output exceeds 1 MiB") + "\n")
457
+ .getBytes(StandardCharsets.UTF_8);
458
+ }
459
+ JSON_OUTPUT.write(bytes, 0, bytes.length);
460
+ JSON_OUTPUT.flush();
461
+ }
462
+
463
+ private record ProjectRecord(String buildRoot, String path, String name, String directory, String parentPath) {
464
+ }
465
+
466
+ private static final class CancellationState {
467
+ private final CancellationTokenSource tokenSource;
468
+ private final CompletableFuture<String> signal = new CompletableFuture<>();
469
+ private String cancellationReason;
470
+ private boolean committed;
471
+
472
+ private CancellationState(CancellationTokenSource tokenSource) {
473
+ this.tokenSource = tokenSource;
474
+ }
475
+
476
+ private synchronized void request(String reason) {
477
+ if (committed || cancellationReason != null) {
478
+ return;
479
+ }
480
+ cancellationReason = reason;
481
+ try {
482
+ tokenSource.cancel();
483
+ } finally {
484
+ signal.complete(reason);
485
+ }
486
+ }
487
+
488
+ private synchronized String reason() {
489
+ return cancellationReason;
490
+ }
491
+
492
+ private synchronized boolean commit() {
493
+ if (cancellationReason != null) {
494
+ return false;
495
+ }
496
+ committed = true;
497
+ return true;
498
+ }
499
+ }
500
+
501
+ private static final class ObservationFailure extends Exception {
502
+ private final String status;
503
+
504
+ private ObservationFailure(String status, String message) {
505
+ super(message);
506
+ this.status = status;
507
+ }
508
+ }
509
+
510
+ private static final class CappedLogOutputStream extends OutputStream {
511
+ private final PrintStream target;
512
+ private final long maxBytes;
513
+ private final AtomicLong written = new AtomicLong();
514
+
515
+ private CappedLogOutputStream(PrintStream target, long maxBytes) {
516
+ this.target = target;
517
+ this.maxBytes = maxBytes;
518
+ }
519
+
520
+ @Override
521
+ public synchronized void write(int value) {
522
+ if (written.get() < maxBytes) {
523
+ target.write(value);
524
+ written.incrementAndGet();
525
+ }
526
+ }
527
+
528
+ @Override
529
+ public synchronized void write(byte[] buffer, int offset, int length) {
530
+ long available = Math.max(0L, maxBytes - written.get());
531
+ int count = (int) Math.min(available, Math.max(0, length));
532
+ if (count > 0) {
533
+ target.write(buffer, offset, count);
534
+ written.addAndGet(count);
535
+ }
536
+ }
537
+
538
+ @Override
539
+ public synchronized void flush() {
540
+ target.flush();
541
+ }
542
+ }
543
+
544
+ private static final class JsonWriter {
545
+ private final StringBuilder value = new StringBuilder();
546
+ private int utf8Bytes;
547
+
548
+ private void raw(String text) {
549
+ int addedBytes = text.getBytes(StandardCharsets.UTF_8).length;
550
+ if ((long) utf8Bytes + addedBytes + 1 > MAX_OUTPUT_BYTES) {
551
+ throw new JsonTooLargeException();
552
+ }
553
+ value.append(text);
554
+ utf8Bytes += addedBytes;
555
+ }
556
+
557
+ private void string(String text) {
558
+ raw("\"");
559
+ for (int index = 0; index < text.length(); index++) {
560
+ char character = text.charAt(index);
561
+ switch (character) {
562
+ case '"' -> raw("\\\"");
563
+ case '\\' -> raw("\\\\");
564
+ case '\b' -> raw("\\b");
565
+ case '\f' -> raw("\\f");
566
+ case '\n' -> raw("\\n");
567
+ case '\r' -> raw("\\r");
568
+ case '\t' -> raw("\\t");
569
+ default -> {
570
+ if (character < 0x20) {
571
+ raw(String.format(Locale.ROOT, "\\u%04x", (int) character));
572
+ } else if (Character.isHighSurrogate(character)
573
+ && index + 1 < text.length()
574
+ && Character.isLowSurrogate(text.charAt(index + 1))) {
575
+ raw(text.substring(index, index + 2));
576
+ index++;
577
+ } else {
578
+ raw(String.valueOf(character));
579
+ }
580
+ }
581
+ }
582
+ }
583
+ raw("\"");
584
+ }
585
+
586
+ @Override
587
+ public String toString() {
588
+ return value.toString();
589
+ }
590
+ }
591
+
592
+ private static final class JsonTooLargeException extends RuntimeException {
593
+ private JsonTooLargeException() {
594
+ super("Observation output exceeds 1 MiB");
595
+ }
596
+ }
597
+ }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mrclrchtr/supi-code-runtime",
3
- "version": "11.0.1",
3
+ "version": "11.2.0",
4
4
  "description": "Shared workspace context and capability contracts for code intelligence",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mrclrchtr/supi-core",
3
- "version": "11.0.1",
3
+ "version": "11.2.0",
4
4
  "description": "Shared settings, configuration, reporting, and session infrastructure",
5
5
  "license": "MIT",
6
6
  "repository": {