@pal-code/core 0.0.21 → 0.0.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/agent/scope.ts +12 -1
- package/src/agent/turn.ts +17 -4
- package/src/app-config.ts +19 -1
- package/src/config.ts +2 -1
- package/src/diagnostics-checks.ts +19 -0
- package/src/index.ts +16 -0
- package/src/lsp/client.ts +361 -0
- package/src/lsp/diagnostics-map.ts +138 -0
- package/src/lsp/fixtures/fake-lsp-server.ts +110 -0
- package/src/lsp/framing.ts +93 -0
- package/src/lsp/servers.ts +125 -0
- package/src/tools/definitions.ts +57 -39
- package/src/tools/edit-match.ts +201 -0
- package/src/tools/executor.ts +517 -92
- package/src/tools/json-schema.ts +341 -0
- package/src/tools/lazy.ts +191 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Edit-match recovery (roadmap #4): whitespace-normalised fallback and
|
|
3
|
+
* candidate reporting.
|
|
4
|
+
*
|
|
5
|
+
* The exact-string matcher is deliberately strict — it refuses a non-unique
|
|
6
|
+
* `old_string` rather than silently changing the wrong site. That strictness is
|
|
7
|
+
* right, but the failure messages gave the model almost nothing to act on:
|
|
8
|
+
*
|
|
9
|
+
* - "not unique (3 occurrences)" — no indication *where*, so the model retried
|
|
10
|
+
* blind or widened a `read` it did not need.
|
|
11
|
+
* - "not found" — no retry at all, even when the only difference was that the
|
|
12
|
+
* file was re-indented or the block was re-wrapped.
|
|
13
|
+
*
|
|
14
|
+
* Both helpers here are pure and operate on strings only, so they are cheap to
|
|
15
|
+
* test and cannot touch the filesystem. Neither ever *writes* — they produce
|
|
16
|
+
* either a candidate list to report or an exact span to replace.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/** One line to show, trimmed and clipped so a long line cannot flood the prompt. */
|
|
20
|
+
const MAX_PREVIEW = 100;
|
|
21
|
+
|
|
22
|
+
export interface OccurrenceSite {
|
|
23
|
+
/** 1-based line of the match start. */
|
|
24
|
+
line: number;
|
|
25
|
+
/** The matched line, trimmed and clipped. */
|
|
26
|
+
preview: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Where does `needle` appear in `content`? One entry per occurrence, in file
|
|
31
|
+
* order, with a 1-based line number.
|
|
32
|
+
*
|
|
33
|
+
* Used to turn "not unique (3 occurrences)" into a list the model can act on.
|
|
34
|
+
* Line numbers are what make the retry possible: the model can read a narrow
|
|
35
|
+
* window around one candidate instead of re-reading the file or guessing.
|
|
36
|
+
*
|
|
37
|
+
* Counts exactly as `content.split(needle).length - 1` does, so the number of
|
|
38
|
+
* sites listed always matches the number the error message states.
|
|
39
|
+
*/
|
|
40
|
+
export function findOccurrences(content: string, needle: string): OccurrenceSite[] {
|
|
41
|
+
const sites: OccurrenceSite[] = [];
|
|
42
|
+
if (needle === '') return sites;
|
|
43
|
+
|
|
44
|
+
let from = 0;
|
|
45
|
+
for (;;) {
|
|
46
|
+
const idx = content.indexOf(needle, from);
|
|
47
|
+
if (idx === -1) break;
|
|
48
|
+
|
|
49
|
+
const lineStart = content.lastIndexOf('\n', idx - 1) + 1;
|
|
50
|
+
const line = content.slice(0, idx).split('\n').length;
|
|
51
|
+
const lineEnd = content.indexOf('\n', idx);
|
|
52
|
+
const text = content.slice(lineStart, lineEnd === -1 ? undefined : lineEnd).trim();
|
|
53
|
+
|
|
54
|
+
sites.push({
|
|
55
|
+
line,
|
|
56
|
+
preview: text.length > MAX_PREVIEW ? `${text.slice(0, MAX_PREVIEW - 3)}...` : text,
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// Advance by the full needle: the uniqueness check is
|
|
60
|
+
// `content.split(needle).length - 1`, which counts *non-overlapping*
|
|
61
|
+
// matches. Advancing by one would list a different number of sites than the
|
|
62
|
+
// message claims, which is worse than no list at all.
|
|
63
|
+
from = idx + needle.length;
|
|
64
|
+
}
|
|
65
|
+
return sites;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Render a candidate list for the error message the model reads.
|
|
70
|
+
*
|
|
71
|
+
* Capped at a handful: the point is to let the model pick a site, not to dump a
|
|
72
|
+
* file. The count is always stated even when the list is truncated.
|
|
73
|
+
*/
|
|
74
|
+
export function formatOccurrenceSites(sites: readonly OccurrenceSite[], max = 5): string {
|
|
75
|
+
const shown = sites.slice(0, max).map((s) => ` ${s.line}: ${s.preview}`);
|
|
76
|
+
const rest = sites.length - shown.length;
|
|
77
|
+
const tail = rest > 0 ? `\n ...and ${rest} more` : '';
|
|
78
|
+
return `Occurrences at:\n${shown.join('\n')}${tail}`;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export interface NormalisedMatch {
|
|
82
|
+
/** Byte offset of the start of the first matched line, *including* its indent. */
|
|
83
|
+
start: number;
|
|
84
|
+
/** Byte offset just past the last matched line's content. */
|
|
85
|
+
end: number;
|
|
86
|
+
/** Leading whitespace of the first matched line, for re-indenting. */
|
|
87
|
+
indent: string;
|
|
88
|
+
/** Leading whitespace of every matched line, in order. */
|
|
89
|
+
indents: string[];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Match `needle` against `content` ignoring indentation and line-ending churn.
|
|
94
|
+
*
|
|
95
|
+
* Worth having because the single most common false "not found" is formatting
|
|
96
|
+
* drift, not a real miss: the model remembers a block, the file was re-indented
|
|
97
|
+
* or re-wrapped, and every byte of the logic still matches. Without this the
|
|
98
|
+
* model has to re-read the file and retry — two wasted rounds for a change that
|
|
99
|
+
* was already known.
|
|
100
|
+
*
|
|
101
|
+
* Deliberately conservative, because a loose matcher that silently rewrites the
|
|
102
|
+
* wrong block is worse than a miss:
|
|
103
|
+
*
|
|
104
|
+
* - **Whole lines only.** Each line is compared trimmed; a needle never matches
|
|
105
|
+
* a mid-line fragment it shares with a longer line.
|
|
106
|
+
* - **Line count must match exactly.** A re-wrapped block is *not* recovered
|
|
107
|
+
* here — that is what the ast-grep structural fallback is for.
|
|
108
|
+
* - **Multi-line needles only.** A single-line needle trimmed is nearly any
|
|
109
|
+
* short string, so allowing it would make "not found" quietly become "found
|
|
110
|
+
* something else". Single-line behavior stays exactly as it is today.
|
|
111
|
+
* - **Exactly one match, or nothing.** Ambiguity returns null and the caller
|
|
112
|
+
* keeps its original error.
|
|
113
|
+
*/
|
|
114
|
+
export function findNormalisedMatch(content: string, needle: string): NormalisedMatch | null {
|
|
115
|
+
if (!needle.includes('\n')) return null;
|
|
116
|
+
|
|
117
|
+
const needleLines = needle.split('\n').map((l) => l.trim());
|
|
118
|
+
// A trailing newline in the needle produces an empty final element that would
|
|
119
|
+
// demand a blank line in the file; drop it so both forms behave the same.
|
|
120
|
+
while (needleLines.length > 0 && needleLines[needleLines.length - 1] === '') {
|
|
121
|
+
needleLines.pop();
|
|
122
|
+
}
|
|
123
|
+
if (needleLines.length < 2) return null;
|
|
124
|
+
|
|
125
|
+
const lines = content.split('\n');
|
|
126
|
+
let hit: NormalisedMatch | null = null;
|
|
127
|
+
|
|
128
|
+
for (let i = 0; i + needleLines.length <= lines.length; i++) {
|
|
129
|
+
let all = true;
|
|
130
|
+
for (let j = 0; j < needleLines.length; j++) {
|
|
131
|
+
if (lines[i + j].trim() !== needleLines[j]) {
|
|
132
|
+
all = false;
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
if (!all) continue;
|
|
137
|
+
|
|
138
|
+
// Two hits is already ambiguous; bail rather than scanning the whole file.
|
|
139
|
+
if (hit !== null) return null;
|
|
140
|
+
|
|
141
|
+
hit = {
|
|
142
|
+
start: offsetOfLine(lines, i),
|
|
143
|
+
// Do not consume the trailing newline: leaving it keeps the file's own
|
|
144
|
+
// line ending intact and avoids joining the next line onto the new text.
|
|
145
|
+
end: offsetOfLine(lines, i) + lines.slice(i, i + needleLines.length).join('\n').length,
|
|
146
|
+
indent: /^[ \t]*/.exec(lines[i])?.[0] ?? '',
|
|
147
|
+
indents: lines.slice(i, i + needleLines.length).map((l) => /^[ \t]*/.exec(l)?.[0] ?? ''),
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return hit;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Re-indent `text` so it sits at `indent`.
|
|
156
|
+
*
|
|
157
|
+
* `findNormalisedMatch` returns a span that begins at the start of the first
|
|
158
|
+
* matched line, so it includes that line's leading whitespace. Replacing that
|
|
159
|
+
* span with the model's raw `new_string` — usually written with no indentation,
|
|
160
|
+
* or with the indentation the model *thought* the file had — would flatten the
|
|
161
|
+
* block against the left margin. That is a silent formatting regression on a
|
|
162
|
+
* path the model is told succeeded, so the new text is shifted to where the old
|
|
163
|
+
* text sat.
|
|
164
|
+
*
|
|
165
|
+
* Everything is expressed relative to the first non-blank line's own indent, so
|
|
166
|
+
* the block's internal structure (nested bodies, continuations) survives.
|
|
167
|
+
* Lines that do not start with that indent are left alone: they are either
|
|
168
|
+
* deliberately misaligned or continuation lines, and rewriting them is guesswork.
|
|
169
|
+
*/
|
|
170
|
+
export function reindentBlock(text: string, indent: string, original?: readonly string[]): string {
|
|
171
|
+
const lines = text.split('\n');
|
|
172
|
+
|
|
173
|
+
// Best case: the replacement has the same line count as what it replaces, so
|
|
174
|
+
// each line can be put at exactly the depth its predecessor sat at. Without
|
|
175
|
+
// this, a replacement written with no indentation would land at the block's
|
|
176
|
+
// *base* indent and silently flatten one level of nesting.
|
|
177
|
+
if (original !== undefined && original.length === lines.length) {
|
|
178
|
+
return lines.map((line, i) => (line.trim() === '' ? '' : original[i] + line.trim())).join('\n');
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const firstContent = lines.find((l) => l.trim() !== '');
|
|
182
|
+
if (firstContent === undefined) return text;
|
|
183
|
+
|
|
184
|
+
const sourceIndent = /^[ \t]*/.exec(firstContent)?.[0] ?? '';
|
|
185
|
+
if (sourceIndent === indent) return text;
|
|
186
|
+
|
|
187
|
+
return lines
|
|
188
|
+
.map((line) => {
|
|
189
|
+
if (line.trim() === '') return ''; // never carry trailing whitespace over
|
|
190
|
+
if (!line.startsWith(sourceIndent)) return line;
|
|
191
|
+
return indent + line.slice(sourceIndent.length);
|
|
192
|
+
})
|
|
193
|
+
.join('\n');
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** Byte offset where line `index` (0-based) starts. */
|
|
197
|
+
function offsetOfLine(lines: readonly string[], index: number): number {
|
|
198
|
+
let offset = 0;
|
|
199
|
+
for (let i = 0; i < index; i++) offset += lines[i].length + 1;
|
|
200
|
+
return offset;
|
|
201
|
+
}
|