@mrclrchtr/supi-code-intelligence 11.4.0 → 12.0.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 (89) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/README.md +13 -7
  3. package/node_modules/@mrclrchtr/supi-code-runtime/package.json +1 -1
  4. package/node_modules/@mrclrchtr/supi-code-runtime/src/api.ts +1 -0
  5. package/node_modules/@mrclrchtr/supi-code-runtime/src/types.ts +51 -23
  6. package/node_modules/@mrclrchtr/supi-core/package.json +1 -1
  7. package/node_modules/@mrclrchtr/supi-lsp/README.md +21 -16
  8. package/node_modules/@mrclrchtr/supi-lsp/node_modules/@mrclrchtr/supi-code-runtime/package.json +1 -1
  9. package/node_modules/@mrclrchtr/supi-lsp/node_modules/@mrclrchtr/supi-code-runtime/src/api.ts +1 -0
  10. package/node_modules/@mrclrchtr/supi-lsp/node_modules/@mrclrchtr/supi-code-runtime/src/types.ts +51 -23
  11. package/node_modules/@mrclrchtr/supi-lsp/node_modules/@mrclrchtr/supi-core/package.json +1 -1
  12. package/node_modules/@mrclrchtr/supi-lsp/package.json +3 -3
  13. package/node_modules/@mrclrchtr/supi-lsp/src/api.ts +2 -2
  14. package/node_modules/@mrclrchtr/supi-lsp/src/client/client-diagnostic-capabilities.ts +13 -3
  15. package/node_modules/@mrclrchtr/supi-lsp/src/client/client-diagnostics.ts +25 -9
  16. package/node_modules/@mrclrchtr/supi-lsp/src/client/client-document-sync.ts +7 -5
  17. package/node_modules/@mrclrchtr/supi-lsp/src/client/client-edit-capture.ts +258 -0
  18. package/node_modules/@mrclrchtr/supi-lsp/src/client/client.ts +64 -11
  19. package/node_modules/@mrclrchtr/supi-lsp/src/config/capabilities.ts +8 -0
  20. package/node_modules/@mrclrchtr/supi-lsp/src/config/operation-support.ts +48 -79
  21. package/node_modules/@mrclrchtr/supi-lsp/src/manager/manager.ts +3 -1
  22. package/node_modules/@mrclrchtr/supi-lsp/src/provider/lsp-refactor-provider.ts +21 -35
  23. package/node_modules/@mrclrchtr/supi-lsp/src/provider/refactor-command-capture.ts +139 -0
  24. package/node_modules/@mrclrchtr/supi-lsp/src/provider/refactor-planning.ts +202 -47
  25. package/node_modules/@mrclrchtr/supi-lsp/src/session/route-boundary.ts +26 -0
  26. package/node_modules/@mrclrchtr/supi-lsp/src/session/runtime-registry.ts +57 -12
  27. package/node_modules/@mrclrchtr/supi-lsp/src/session/runtime-rename.ts +3 -0
  28. package/node_modules/@mrclrchtr/supi-lsp/src/session/workspace-lsp-runtime.ts +22 -1
  29. package/node_modules/@mrclrchtr/supi-tree-sitter/node_modules/@mrclrchtr/supi-code-runtime/package.json +1 -1
  30. package/node_modules/@mrclrchtr/supi-tree-sitter/node_modules/@mrclrchtr/supi-code-runtime/src/api.ts +1 -0
  31. package/node_modules/@mrclrchtr/supi-tree-sitter/node_modules/@mrclrchtr/supi-code-runtime/src/types.ts +51 -23
  32. package/node_modules/@mrclrchtr/supi-tree-sitter/node_modules/@mrclrchtr/supi-core/package.json +1 -1
  33. package/node_modules/@mrclrchtr/supi-tree-sitter/package.json +3 -3
  34. package/node_modules/@mrclrchtr/supi-tree-sitter/src/coordinates.ts +16 -2
  35. package/node_modules/@mrclrchtr/supi-tree-sitter/src/session/structural-worker-client.ts +3 -2
  36. package/node_modules/@mrclrchtr/supi-tree-sitter/src/syntax-errors.ts +2 -0
  37. package/node_modules/@mrclrchtr/supi-tree-sitter/src/tool/call-range.ts +94 -0
  38. package/node_modules/@mrclrchtr/supi-tree-sitter/src/tool/call-sites.ts +26 -6
  39. package/node_modules/@mrclrchtr/supi-tree-sitter/src/tool/callees.ts +6 -3
  40. package/node_modules/@mrclrchtr/supi-tree-sitter/src/tool/exports.ts +13 -4
  41. package/node_modules/@mrclrchtr/supi-tree-sitter/src/worker/runtime.ts +1 -9
  42. package/package.json +5 -5
  43. package/src/analysis/health/server-status.ts +41 -12
  44. package/src/analysis/target/anchored-structural.ts +4 -1
  45. package/src/analysis/target/symbol.ts +1 -1
  46. package/src/session/find-types.ts +8 -4
  47. package/src/session/find-workflow.ts +11 -16
  48. package/src/session/input/health-refactor.ts +48 -34
  49. package/src/session/input/workflows.ts +45 -46
  50. package/src/session/inspect/file-point.ts +2 -2
  51. package/src/session/inspect-types.ts +1 -1
  52. package/src/session/inspect-workflow.ts +3 -3
  53. package/src/session/refactor-file-actions.ts +122 -0
  54. package/src/session/refactor-plan-pipeline.ts +152 -0
  55. package/src/session/refactor-plans.ts +18 -15
  56. package/src/session/refactor-types.ts +24 -12
  57. package/src/session/refactor-workflow.ts +68 -243
  58. package/src/tool/code_find/execute.ts +6 -7
  59. package/src/tool/code_find/spec.ts +9 -16
  60. package/src/tool/code_find/tui.ts +12 -6
  61. package/src/tool/code_graph/spec.ts +6 -0
  62. package/src/tool/code_health/spec.ts +6 -0
  63. package/src/tool/code_health/structured-schema.ts +4 -4
  64. package/src/tool/code_health/structured.ts +15 -17
  65. package/src/tool/code_inspect/execute.ts +1 -1
  66. package/src/tool/code_inspect/markdown.ts +33 -6
  67. package/src/tool/code_inspect/result.ts +1 -1
  68. package/src/tool/code_inspect/spec.ts +14 -6
  69. package/src/tool/code_inspect/structured.ts +2 -2
  70. package/src/tool/code_inspect/tui.ts +2 -2
  71. package/src/tool/code_orientation/spec.ts +8 -0
  72. package/src/tool/code_refactor_apply/result.ts +2 -1
  73. package/src/tool/code_refactor_apply/spec.ts +7 -0
  74. package/src/tool/code_refactor_plan/execute.ts +0 -2
  75. package/src/tool/code_refactor_plan/result.ts +23 -13
  76. package/src/tool/code_refactor_plan/spec.ts +8 -2
  77. package/src/tool/code_refactor_plan/structured.ts +59 -12
  78. package/src/tool/code_refactor_plan/tui.ts +14 -17
  79. package/src/tool/code_resolve/markdown.ts +1 -1
  80. package/src/tool/code_resolve/spec.ts +6 -0
  81. package/src/tool/namespace.ts +14 -0
  82. package/src/tool/refactor-markdown.ts +55 -15
  83. package/src/tool/register.ts +3 -0
  84. package/src/tool/result/refactor-preview.ts +118 -0
  85. package/src/tool/result/refactor-unavailable.ts +16 -0
  86. package/src/tool/result/refactor.ts +8 -2
  87. package/src/tool/schemas.ts +63 -20
  88. package/src/tool/specs.ts +3 -0
  89. package/src/ui/status-overlay.ts +1 -1
@@ -0,0 +1,118 @@
1
+ /**
2
+ * Readable preview entries for refactor plans.
3
+ *
4
+ * Some servers (kotlin-lsp) return minimal character-level edits, so renaming
5
+ * `total` to `sumDoubled` arrives as `sumD`, `ub`, `ed`. The preview combines
6
+ * consecutive single-line edits on one line into one before/after entry. The
7
+ * stored plan and its edits stay exactly as the server returned them.
8
+ */
9
+
10
+ import { readFileSync } from "node:fs";
11
+ import type { FileEdit } from "@mrclrchtr/supi-code-runtime/api";
12
+
13
+ /** One preview entry: a server edit as is, or several edits on one line combined. */
14
+ export type RefactorPreviewEntry =
15
+ | { readonly kind: "edit"; readonly edit: FileEdit }
16
+ | {
17
+ readonly kind: "line";
18
+ readonly file: string;
19
+ /** Zero-based line and UTF-16 span covering every combined edit. */
20
+ readonly line: number;
21
+ readonly startCharacter: number;
22
+ readonly endCharacter: number;
23
+ readonly before: string;
24
+ readonly after: string;
25
+ readonly editCount: number;
26
+ };
27
+
28
+ /**
29
+ * Number of leading edits to show so that at least `maxResults` are shown and
30
+ * the last shown edit does not split a group of edits on the same line.
31
+ */
32
+ export function previewEditCount(edits: readonly FileEdit[], maxResults: number): number {
33
+ let count = Math.min(Math.max(0, maxResults), edits.length);
34
+ while (count > 0 && count < edits.length) {
35
+ const last = edits[count - 1] as FileEdit;
36
+ const next = edits[count] as FileEdit;
37
+ if (!isInlineEdit(last) || !isInlineEdit(next) || !sameLine(last, next)) break;
38
+ count++;
39
+ }
40
+ return count;
41
+ }
42
+
43
+ /** Combine consecutive edits on one line when the current file text allows it. */
44
+ export function buildRefactorPreview(edits: readonly FileEdit[]): RefactorPreviewEntry[] {
45
+ const lines = new Map<string, readonly string[] | null>();
46
+ const entries: RefactorPreviewEntry[] = [];
47
+ for (let index = 0; index < edits.length; ) {
48
+ let end = index + 1;
49
+ const first = edits[index] as FileEdit;
50
+ while (
51
+ end < edits.length &&
52
+ isInlineEdit(first) &&
53
+ isInlineEdit(edits[end] as FileEdit) &&
54
+ sameLine(first, edits[end] as FileEdit)
55
+ ) {
56
+ end++;
57
+ }
58
+ const group = edits.slice(index, end);
59
+ const combined = group.length > 1 ? combineLine(group, fileLines(first.file, lines)) : null;
60
+ if (combined) entries.push(combined);
61
+ else entries.push(...group.map((edit) => ({ kind: "edit" as const, edit })));
62
+ index = end;
63
+ }
64
+ return entries;
65
+ }
66
+
67
+ function isInlineEdit(edit: FileEdit): boolean {
68
+ return edit.range.start.line === edit.range.end.line && !/[\r\n]/u.test(edit.newText);
69
+ }
70
+
71
+ function sameLine(a: FileEdit, b: FileEdit): boolean {
72
+ return a.file === b.file && a.range.start.line === b.range.start.line;
73
+ }
74
+
75
+ function fileLines(
76
+ file: string,
77
+ cache: Map<string, readonly string[] | null>,
78
+ ): readonly string[] | null {
79
+ if (!cache.has(file)) {
80
+ try {
81
+ cache.set(file, readFileSync(file, "utf8").split(/\r\n|\r|\n/u));
82
+ } catch {
83
+ cache.set(file, null);
84
+ }
85
+ }
86
+ return cache.get(file) ?? null;
87
+ }
88
+
89
+ /** Apply sorted, non-overlapping edits to their line; null when that is not possible. */
90
+ function combineLine(
91
+ group: readonly FileEdit[],
92
+ lines: readonly string[] | null,
93
+ ): RefactorPreviewEntry | null {
94
+ const first = group[0] as FileEdit;
95
+ const text = lines?.[first.range.start.line];
96
+ if (text === undefined) return null;
97
+ const sorted = [...group].sort((a, b) => a.range.start.character - b.range.start.character);
98
+ let after = "";
99
+ let cursor = (sorted[0] as FileEdit).range.start.character;
100
+ const startCharacter = cursor;
101
+ for (const edit of sorted) {
102
+ const { start, end } = edit.range;
103
+ if (start.character < cursor || end.character < start.character) return null;
104
+ if (end.character > text.length) return null;
105
+ after += text.slice(cursor, start.character) + edit.newText;
106
+ cursor = end.character;
107
+ }
108
+ return {
109
+ kind: "line",
110
+ file: first.file,
111
+ line: first.range.start.line,
112
+ startCharacter,
113
+ endCharacter: cursor,
114
+ before: text.slice(startCharacter, cursor),
115
+ after,
116
+ editCount: group.length,
117
+ };
118
+ }
@@ -0,0 +1,16 @@
1
+ /** Unavailable reasons shared by code_refactor_plan and code_refactor_apply. */
2
+
3
+ /** supi-lsp text for a request superseded by a file change while it ran. */
4
+ const SEMANTIC_INPUT_CHANGED = "Semantic input changed";
5
+ const CONCURRENT_EDIT_HINT = "Retry after concurrent edits finish.";
6
+
7
+ /**
8
+ * Add a recovery hint to a reason caused by a concurrent edit, for example a
9
+ * plan running in parallel with an apply. Other reasons are returned unchanged.
10
+ */
11
+ export function refactorUnavailableReason(reason: string): string {
12
+ if (!reason.includes(SEMANTIC_INPUT_CHANGED) || reason.includes(CONCURRENT_EDIT_HINT)) {
13
+ return reason;
14
+ }
15
+ return `${reason} ${CONCURRENT_EDIT_HINT}`;
16
+ }
@@ -5,6 +5,7 @@ import { toDisplayPath } from "../../analysis/search/paths.ts";
5
5
  import type { RefactorPlan } from "../../session/refactor-plans.ts";
6
6
  import { assembledNextQueries, assembleToolResult, type ToolResultAssembly } from "./assembly.ts";
7
7
  import { createToolDisplaySection } from "./display.ts";
8
+ import { previewEditCount } from "./refactor-preview.ts";
8
9
  import type { SearchDetails, ToolDisplaySection } from "./types.ts";
9
10
 
10
11
  export interface RefactorPlanAssemblyData {
@@ -45,7 +46,7 @@ export function assembleRefactorPlanDetails(
45
46
  const edits = createEvidenceList({
46
47
  key: "refactor.edits",
47
48
  items: [...plan.edits.edits],
48
- maxResults,
49
+ maxResults: previewEditCount(plan.edits.edits, maxResults),
49
50
  });
50
51
  const evidenceSource = plan.evidenceSource ?? "semantic";
51
52
  const provenance = [
@@ -67,7 +68,12 @@ export function assembleRefactorPlanDetails(
67
68
  },
68
69
  ],
69
70
  evidenceLists: [edits.metadata],
70
- nextQueries: [`Use code_refactor_apply with planId: "${plan.id}" to apply this refactor`],
71
+ nextQueries: [
72
+ `Use code_refactor_apply with planId: "${plan.id}" to apply this refactor`,
73
+ ...(plan.operation === "code_action"
74
+ ? ["After applying, use rename_symbol to rename any server-generated names"]
75
+ : []),
76
+ ],
71
77
  candidateCount: edits.metadata.totalCount ?? edits.metadata.shownCount,
72
78
  confidence: evidenceSource,
73
79
  provenance,
@@ -4,11 +4,12 @@ import {
4
4
  type PublicRefactorOperationName,
5
5
  } from "../session/refactor-types.ts";
6
6
  import { TARGET_SYMBOL_KINDS } from "../session/target-input.ts";
7
+ import { CODE_FIND_AST_KINDS } from "./code_find/ast-kinds.ts";
7
8
 
8
9
  export const ScopeParam = Type.String({
9
10
  description: "Workspace-relative file or directory scope.",
10
11
  });
11
- export const FindScopeParam = Type.Array(
12
+ export const FindScopesParam = Type.Array(
12
13
  Type.String({ description: "Workspace-relative search scope." }),
13
14
  {
14
15
  description: "One or more workspace-relative search scopes.",
@@ -20,11 +21,6 @@ export const QueryParam = Type.String({
20
21
  description: "Human or code reference to resolve or search for.",
21
22
  minLength: 1,
22
23
  });
23
- export const FindQueryParam = Type.String({
24
- description:
25
- "AST mode: name or import module-specifier substring. Semantic mode: workspace-symbol query.",
26
- minLength: 1,
27
- });
28
24
  const LineParam = Type.Integer({ description: "1-based line.", minimum: 1 });
29
25
  const CharacterParam = Type.Integer({
30
26
  description: "1-based UTF-16 column.",
@@ -74,6 +70,29 @@ function exactOneSelector(properties: Record<string, TSchema>, description: stri
74
70
  });
75
71
  }
76
72
 
73
+ export const FindSearchParam = exactOneSelector(
74
+ {
75
+ ast: Type.Object(
76
+ {
77
+ kind: Type.Enum(CODE_FIND_AST_KINDS, {
78
+ type: "string",
79
+ description: "AST structure kind.",
80
+ }),
81
+ query: Type.String({
82
+ description: "Name or import module-specifier substring.",
83
+ minLength: 1,
84
+ }),
85
+ },
86
+ { additionalProperties: false, description: "Structural search over syntax trees." },
87
+ ),
88
+ semantic: Type.Object(
89
+ { query: Type.String({ description: "Workspace-symbol query.", minLength: 1 }) },
90
+ { additionalProperties: false, description: "Language-server workspace-symbol search." },
91
+ ),
92
+ },
93
+ "Exactly one search mode.",
94
+ );
95
+
77
96
  export const ResolveTargetParam = exactOneSelector(
78
97
  {
79
98
  anchor: SourcePointParam,
@@ -112,7 +131,7 @@ export const OrientationFocusParam = exactOneSelector(
112
131
  "Exactly one Orientation focus.",
113
132
  );
114
133
 
115
- export const RefactorTargetParam = exactOneSelector(
134
+ const RefactorTargetParam = exactOneSelector(
116
135
  {
117
136
  handle: Type.String({ description: "Target handle from a previous code tool result." }),
118
137
  anchor: SourcePointParam,
@@ -131,28 +150,52 @@ const RangeParam = Type.Object(
131
150
  { additionalProperties: false },
132
151
  ),
133
152
  },
134
- { additionalProperties: false },
153
+ {
154
+ additionalProperties: false,
155
+ description: "Source range; end is exclusive. Equal start and end mark a cursor position.",
156
+ },
135
157
  );
136
158
 
137
159
  const NewNameParam = Type.String({ description: "New symbol name.", minLength: 1 });
160
+ const ActionTitleParam = Type.String({
161
+ description:
162
+ "Exact action title from a previous ambiguous result; selects one of several matching actions.",
163
+ minLength: 1,
164
+ });
138
165
 
139
166
  const RefactorOperationProperties = {
140
- rename_symbol: Type.Object({ newName: NewNameParam }, { additionalProperties: false }),
141
- extract_function: Type.Object(
142
- { newName: NewNameParam, range: RangeParam },
143
- { additionalProperties: false },
144
- ),
145
- extract_variable: Type.Object(
146
- { newName: NewNameParam, range: RangeParam },
147
- { additionalProperties: false },
167
+ rename_symbol: Type.Object(
168
+ { target: RefactorTargetParam, newName: NewNameParam },
169
+ { additionalProperties: false, description: "Rename one symbol across the workspace." },
148
170
  ),
149
- update_imports: Type.Object({}, { additionalProperties: false }),
150
- delete_dead_code: Type.Object(
151
- {},
171
+ code_action: Type.Object(
172
+ {
173
+ file: FileParam,
174
+ range: RangeParam,
175
+ kind: Type.String({
176
+ description:
177
+ 'CodeActionKind prefix under "refactor", such as "refactor.extract" or "refactor.inline". Sub-kinds match.',
178
+ pattern: "^refactor(\\..+)?$",
179
+ }),
180
+ title: Type.Optional(ActionTitleParam),
181
+ },
152
182
  {
153
183
  additionalProperties: false,
154
- description: "Remove server-supported unused code; support may be limited to unused imports.",
184
+ description:
185
+ "Server refactor action for one range. Generated names (e.g. extracted variables) appear in the preview; rename them afterwards with rename_symbol.",
186
+ },
187
+ ),
188
+ source_action: Type.Object(
189
+ {
190
+ file: FileParam,
191
+ kind: Type.String({
192
+ description:
193
+ 'CodeActionKind prefix under "source", such as "source.organizeImports" or "source.fixAll". Sub-kinds match.',
194
+ pattern: "^source(\\..+)?$",
195
+ }),
196
+ title: Type.Optional(ActionTitleParam),
155
197
  },
198
+ { additionalProperties: false, description: "Server source action for one whole file." },
156
199
  ),
157
200
  } satisfies Record<PublicRefactorOperationName, TSchema>;
158
201
 
package/src/tool/specs.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { ToolAnnotations } from "@earendil-works/pi-coding-agent";
1
2
  import type { Component } from "@earendil-works/pi-tui";
2
3
  import type { TSchema } from "typebox";
3
4
  import type {
@@ -27,6 +28,8 @@ export type CodeToolRendererFn = (...args: any[]) => Component;
27
28
  export interface CodeIntelligenceToolDefinitionSpec {
28
29
  name: CodeIntelligenceToolName;
29
30
  label: string;
31
+ /** Audited behavior hints for permission extensions (MCP tool-annotation semantics). */
32
+ annotations: ToolAnnotations;
30
33
  parameters: TSchema;
31
34
  /** Machine-readable success and recoverable-input outcomes for programmatic callers. */
32
35
  outputSchema?: TSchema;
@@ -618,7 +618,7 @@ export class CiStatusDialog {
618
618
  text(
619
619
  t.fg(
620
620
  "dim",
621
- ` ${operation}: server ${support.server}; SuPi ${support.supi}${support.reason ? ` — ${support.reason}` : ""}`,
621
+ ` ${operation}: server ${support.server}; SuPi ${support.supi}${support.kinds?.length ? `; kinds ${support.kinds.join(", ")}` : ""}${support.anyKind ? "; may return any kind" : ""}${support.reason ? ` — ${support.reason}` : ""}`,
622
622
  ),
623
623
  );
624
624
  }