@plannotator/sem-jiff 0.2.0 → 0.2.2
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/CHANGELOG.md +41 -0
- package/README.md +20 -8
- package/dist/index.d.ts +6 -3
- package/dist/index.js +94 -6
- package/dist/inline.js +245 -9
- package/dist/linediff.d.ts +2 -1
- package/dist/linediff.js +44 -2
- package/dist/pairing.d.ts +1 -1
- package/dist/pairing.js +137 -5
- package/dist/parse.d.ts +4 -3
- package/dist/parse.js +27 -8
- package/dist/runtime.d.ts +2 -0
- package/dist/runtime.js +9 -0
- package/dist/structure.d.ts +11 -1
- package/dist/structure.js +222 -5
- package/dist/symbol-types.d.ts +18 -0
- package/dist/symbol-types.js +1 -0
- package/dist/symbols.d.ts +6 -0
- package/dist/symbols.js +458 -0
- package/dist/syntax.d.ts +3 -1
- package/dist/syntax.js +42 -4
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,46 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.2.2 (trust release)
|
|
4
|
+
|
|
5
|
+
Two safety fixes from the held4 blind set (110 projects, 11 languages). Both only take claims away.
|
|
6
|
+
|
|
7
|
+
- `reformatted` now needs the same words, inside strings and docstrings too. A Python string holding an escape
|
|
8
|
+
sequence (`\\_`, `\n`) parses as the escape alone, so the words around it were never compared: reworded
|
|
9
|
+
docstrings, deleted `.. versionadded::` notes and renamed doctest variables were called formatting (58 of 150
|
|
10
|
+
sampled Python claims wrong). Rewrapping keeps the claim; changing a word makes the lines plain edits. Rule
|
|
11
|
+
`REFORMAT_WORDS`.
|
|
12
|
+
- Moved-with-edits is held back in every language: about 1 in 3 sampled claims paired two different functions or
|
|
13
|
+
blocks (Go about 8%, Kotlin about 63%). Its lines show as plain edits.
|
|
14
|
+
|
|
15
|
+
Also in the code, switched off: eleven trial rules (`SEMANTIC_TRY_*`) that were scored on the same blind set and did
|
|
16
|
+
not pass. They change nothing unless switched on.
|
|
17
|
+
|
|
18
|
+
Packaging: published to npm again (0.2.1 was not published; this version includes its changes). The declarations
|
|
19
|
+
for `.` no longer import web-tree-sitter's types through the `symbol` types (now in `dist/symbol-types.d.ts`), so a
|
|
20
|
+
consumer with `skipLibCheck` off again needs no `@types/emscripten`.
|
|
21
|
+
|
|
22
|
+
## 0.2.1
|
|
23
|
+
|
|
24
|
+
Held back in Swift, Ruby and Kotlin: claims whose wrong rate, measured on a fresh blind set, could be above our
|
|
25
|
+
1-in-50 bar. Their lines show as plain edits. The set: 30 projects never used before (10 per language), every claim
|
|
26
|
+
labelled blind by two raters and a third where they disagreed; the bar is the one-sided 95% upper bound on the wrong
|
|
27
|
+
rate, at most 2%.
|
|
28
|
+
|
|
29
|
+
| Language | Held back (wrong in n, 95% upper bound) | Still claimed |
|
|
30
|
+
|---|---|---|
|
|
31
|
+
| Swift | moved 4 in 131 (6.9%), renamed 3 in 149 (5.1%), extract/inline 6 in 142 (8.2%) | reformatted (0 in 150) |
|
|
32
|
+
| Ruby | moved 2 in 96 (6.4%), renamed 2 in 146 (4.3%), extract/inline 2 in 145 (4.3%) | reformatted (0 in 150) |
|
|
33
|
+
| Kotlin | renamed 2 in 143 (4.3%), extract/inline 4 in 142 (6.3%) | moved (0 in 138, under 2.2%), reformatted (0 in 150) |
|
|
34
|
+
|
|
35
|
+
Moved-with-edits stays held back in all three (wrong 30 in 125, 7 in 50, 69 in 131). No second opinion is asked
|
|
36
|
+
about an extract or inline a language holds back. 0.3 brings these back once each is measured under the bar.
|
|
37
|
+
The other eight languages are unchanged.
|
|
38
|
+
|
|
39
|
+
- Each operation can carry an optional `symbol: { name, kind, oldName?, container? }`: the declaration it is about
|
|
40
|
+
(docs/symbols.md). Additive: no other field changes, and it is left out when the engine is unsure.
|
|
41
|
+
- Docs: `copyWasm` in the README; the `sem-jiff-copy-wasm` bin needs Node 24 or later, or Bun; vendoring the source
|
|
42
|
+
tree needs the devDependencies (PACKAGING.md).
|
|
43
|
+
|
|
3
44
|
## 0.2.0 (2026-11-15)
|
|
4
45
|
|
|
5
46
|
The engine Plannotator's code review runs, now as one package. Languages, and the claims each holds back in this version
|
package/README.md
CHANGED
|
@@ -28,6 +28,12 @@ Example, rendered in Plannotator:<br/>
|
|
|
28
28
|
It prefers saying nothing to saying something wrong: a missed change falls back to
|
|
29
29
|
the plain diff, while a wrong label would mislead the reviewer.
|
|
30
30
|
|
|
31
|
+
**Held back.** A fresh blind test on 30 new Swift, Ruby and Kotlin projects found some labels wrong more
|
|
32
|
+
often than our 1-in-50 bar, so for now those lines show as plain edits:
|
|
33
|
+
Swift and Ruby moves, renames and extracts; Kotlin renames and extracts.
|
|
34
|
+
Since 0.2.2, "moved with edits" is held back in every language, and "reformatted" needs the same words,
|
|
35
|
+
docstrings included. Numbers in [CHANGELOG.md](CHANGELOG.md). Each comes back once it passes the bar.
|
|
36
|
+
|
|
31
37
|
## Install
|
|
32
38
|
|
|
33
39
|
```sh
|
|
@@ -48,26 +54,31 @@ const { semanticOperations, semanticFolds, patch } =
|
|
|
48
54
|
```
|
|
49
55
|
|
|
50
56
|
In Node nothing else is needed. In a browser, copy the grammars next to your app
|
|
51
|
-
(`npx sem-jiff-copy-wasm <dir
|
|
57
|
+
(`npx sem-jiff-copy-wasm <dir>`, which needs Node 24+ or Bun, or `copyWasm(dir)` from `@plannotator/sem-jiff/copy-wasm`) and tell the engine where they are served from with
|
|
52
58
|
`configure({ locateGrammar, locateRuntime })`. Options, `reviewSummary` and browser setup: [docs/usage.md](docs/usage.md).
|
|
53
59
|
|
|
54
60
|
## Power Up: Jev and Clef
|
|
55
61
|
|
|
56
62
|
The rules are strict, so they miss extracted functions whose code was reworded on the way out.
|
|
57
|
-
|
|
58
|
-
Jev (via TypeSafe) decides, and Clef (Cloudflare) breaks the tie when Jev is unsure.
|
|
63
|
+
Add a decision model and the engine asks it about exactly those candidates, once per file.
|
|
59
64
|
Your key, your account; call it from a server or your own machine.
|
|
60
65
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
66
|
+
**Try it with Jev** (by TypeSafe):
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
import { jevOpinion } from '@plannotator/sem-jiff/opinions';
|
|
70
|
+
|
|
71
|
+
const diff = await semanticDiffFile('typescript', oldText, newText, 'src/cart.ts', {
|
|
72
|
+
secondOpinion: jevOpinion({ apiKey }),
|
|
73
|
+
});
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
**Or with both:** Clef (Cloudflare) breaks the tie when Jev is unsure.
|
|
65
77
|
|
|
66
78
|
```ts
|
|
67
79
|
import { jevOpinion, clefScore } from '@plannotator/sem-jiff/opinions';
|
|
68
80
|
|
|
69
81
|
const secondOpinion = jevOpinion({ apiKey, tieBreaker: { ask: clefScore({ accountId, apiToken }) } });
|
|
70
|
-
const diff = await semanticDiffFile('typescript', oldText, newText, 'src/cart.ts', { secondOpinion });
|
|
71
82
|
```
|
|
72
83
|
|
|
73
84
|
<picture>
|
|
@@ -88,6 +99,7 @@ costs nothing. More in [docs/usage.md](docs/usage.md#second-opinion-power-up).
|
|
|
88
99
|
On TypeScript practice commits (187 real cases): RefactoringMiner 73 found, 14 wrong; sem-jiff 105, 1 wrong;
|
|
89
100
|
with Jev 148, 1 wrong. Every comparison: [harness/compare/README.md](harness/compare/README.md).
|
|
90
101
|
How the engine is tested: [docs/verification.md](docs/verification.md).
|
|
102
|
+
The Kotlin and Swift figures above are from earlier held-out sets; the newer blind set is in the 0.2.1 notes.
|
|
91
103
|
|
|
92
104
|
## Languages
|
|
93
105
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
import type { LanguageId } from './languages.js';
|
|
2
|
-
import type { SemanticDiffFold
|
|
2
|
+
import type { SemanticDiffFold } from './pierre-types.js';
|
|
3
|
+
import type { SemDiffOperation } from './symbol-types.js';
|
|
3
4
|
export type { SemanticDiffFold, SemanticDiffOperation } from './pierre-types.js';
|
|
4
5
|
export { languageForPath, type LanguageId } from './languages.js';
|
|
6
|
+
export type { OperationSymbol, SemDiffOperation, SymbolKind } from './symbol-types.js';
|
|
5
7
|
export { configure, type Candidate, type SemDiffSetup, type WasmSource } from './runtime.js';
|
|
6
8
|
export { reviewSummary, READ_KINDS, SKIM_KINDS, type ReviewSummary } from './summary.js';
|
|
7
9
|
import type { Candidate } from './runtime.js';
|
|
8
10
|
import { type OpinionAnswer, type OpinionBand } from './opinions.js';
|
|
9
11
|
export interface SemanticFileDiff {
|
|
10
|
-
|
|
12
|
+
/** Pierre's operations, each with the optional `symbol` it is about (docs/symbols.md). */
|
|
13
|
+
semanticOperations: SemDiffOperation[];
|
|
11
14
|
semanticFolds: SemanticDiffFold[];
|
|
12
15
|
/**
|
|
13
16
|
* Unified diff built from the declaration-aware line diff. Give it to your
|
|
@@ -73,4 +76,4 @@ export type Sides = {
|
|
|
73
76
|
/** A stable id for a candidate: the same code always gets the same id. */
|
|
74
77
|
export declare function candidateId(c: Candidate): string;
|
|
75
78
|
export declare function semanticDiffFile(language: LanguageId, oldText: string, newText: string, name?: string, options?: DiffOptions): Promise<SemanticFileDiff>;
|
|
76
|
-
export declare function semanticDiff(language: LanguageId, oldText: string, newText: string): Promise<
|
|
79
|
+
export declare function semanticDiff(language: LanguageId, oldText: string, newText: string): Promise<SemDiffOperation[]>;
|
package/dist/index.js
CHANGED
|
@@ -6,13 +6,14 @@ import { detectStructure } from "./structure.js";
|
|
|
6
6
|
import { uncoveredChanges } from "./coverage.js";
|
|
7
7
|
import { endLine, startLine } from "./syntax.js";
|
|
8
8
|
import { detectEdits, replacementsFromBlocks } from "./edits.js";
|
|
9
|
-
import { alignedLineDiff, diffLines, toPatch } from "./linediff.js";
|
|
9
|
+
import { alignedLineDiff, diffLines, toPatch, unchangedLinePairs } from "./linediff.js";
|
|
10
10
|
import { computeTextDiff, WORK_PER_SECOND } from "./textdiff.js";
|
|
11
11
|
import { detectTextMoves } from "./textmoves.js";
|
|
12
|
+
import { attachSymbols } from "./symbols.js";
|
|
12
13
|
export { languageForPath } from "./languages.js";
|
|
13
14
|
export { configure } from "./runtime.js";
|
|
14
15
|
export { reviewSummary, READ_KINDS, SKIM_KINDS } from "./summary.js";
|
|
15
|
-
import { flags } from "./runtime.js";
|
|
16
|
+
import { flags, rule, trial } from "./runtime.js";
|
|
16
17
|
import { OpinionKeyError } from "./opinions.js";
|
|
17
18
|
const DEFAULT_MAX_CHARS = 2_000_000;
|
|
18
19
|
const DEFAULT_TIME_BUDGET_MS = Number.POSITIVE_INFINITY;
|
|
@@ -277,6 +278,49 @@ function contextChanged(op, cfg, oldRoot, newRoot, oldLines, newLines, at = cfg.
|
|
|
277
278
|
const after = contexts(newRoot, newLines, op.lines.new);
|
|
278
279
|
return before.length !== after.length || before.some((c, i) => c !== after[i]);
|
|
279
280
|
}
|
|
281
|
+
/**
|
|
282
|
+
* Extends each move (moved, movedWithEdit, one range per side) over the blank lines directly above and below it on
|
|
283
|
+
* that side, when they are changed lines no other operation owns and not inside a multi-line string. The root span
|
|
284
|
+
* and its line pair follow the range, so the move draws them as its own. Nothing else changes.
|
|
285
|
+
*/
|
|
286
|
+
function attachBlankSeparators(ops, changed, inString, oldLines, newLines) {
|
|
287
|
+
const owned = { old: new Set(), new: new Set() };
|
|
288
|
+
for (const op of ops)
|
|
289
|
+
for (const side of ['old', 'new'])
|
|
290
|
+
for (const l of op.lines[side])
|
|
291
|
+
for (let n = l.start; n <= l.end; n++)
|
|
292
|
+
owned[side].add(n);
|
|
293
|
+
for (const op of ops) {
|
|
294
|
+
if (op.type !== 'moved' && op.type !== 'movedWithEdit')
|
|
295
|
+
continue;
|
|
296
|
+
for (const side of ['old', 'new']) {
|
|
297
|
+
if (op.lines[side].length !== 1)
|
|
298
|
+
continue;
|
|
299
|
+
const r = op.lines[side][0];
|
|
300
|
+
const lines = side === 'old' ? oldLines : newLines;
|
|
301
|
+
const free = (n) => n >= 1 && n <= lines.length && !(lines[n - 1] ?? '').trim() && changed[side].has(n) && !owned[side].has(n) && !inString[side].has(n);
|
|
302
|
+
let start = r.start;
|
|
303
|
+
let end = r.end;
|
|
304
|
+
while (free(start - 1))
|
|
305
|
+
owned[side].add(--start);
|
|
306
|
+
while (free(end + 1))
|
|
307
|
+
owned[side].add(++end);
|
|
308
|
+
if (start === r.start && end === r.end)
|
|
309
|
+
continue;
|
|
310
|
+
r.start = start;
|
|
311
|
+
r.end = end;
|
|
312
|
+
const root = op.spans[r.spanIndex];
|
|
313
|
+
if (root && root.role === 'root' && 'linePairs' in root) {
|
|
314
|
+
const ranges = root[side];
|
|
315
|
+
if (ranges?.length === 1)
|
|
316
|
+
ranges[0] = { start: { line: start, character: 0 }, end: { line: end + 1, character: 0 } };
|
|
317
|
+
const pair = root.linePairs?.length === 1 ? root.linePairs[0] : undefined;
|
|
318
|
+
if (pair)
|
|
319
|
+
pair[side] = { start, end };
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
280
324
|
export async function semanticDiffFile(language, oldText, newText, name = 'file', options = {}) {
|
|
281
325
|
const cfg = LANGUAGES[language];
|
|
282
326
|
const started = performance.now();
|
|
@@ -297,7 +341,9 @@ export async function semanticDiffFile(language, oldText, newText, name = 'file'
|
|
|
297
341
|
detectExtracted(a.rootNode, b.rootNode, newText, cfg, collect);
|
|
298
342
|
});
|
|
299
343
|
const rejected = new Set(options.rejectedCandidates ?? []);
|
|
300
|
-
|
|
344
|
+
// A held-back extract or inline is never shown: no model is asked about it.
|
|
345
|
+
const held = (c) => rule('HOLD_BACK') && !!cfg.holdBack?.has(c.kind === 'extract' ? 'extractedFunction' : 'inlinedFunction');
|
|
346
|
+
const toAsk = asked.filter((c) => !rejected.has(candidateId(c)) && !held(c));
|
|
301
347
|
const answers = toAsk.length ? await options.secondOpinion(toAsk) : [];
|
|
302
348
|
toAsk.forEach((c, i) => {
|
|
303
349
|
const a = answers[i];
|
|
@@ -350,7 +396,9 @@ export async function semanticDiffFile(language, oldText, newText, name = 'file'
|
|
|
350
396
|
};
|
|
351
397
|
const noInline = minified(oldText) || minified(newText) ? 'minified code' : nestingDepth(oldText) > 80 || nestingDepth(newText) > 80 ? 'nesting too deep' : undefined;
|
|
352
398
|
const inlined = run('inline-extract', () => [...detectInlined(a.rootNode, b.rootNode, oldText, cfg, inlineOptions), ...detectExtracted(a.rootNode, b.rootNode, newText, cfg, inlineOptions)], [], noInline);
|
|
353
|
-
|
|
399
|
+
// Trial DUP_TIEBREAK: the structure pass breaks ties between duplicates by the line diff's unchanged lines.
|
|
400
|
+
const keptLines = trial('DUP_TIEBREAK') ? unchangedLinePairs(alignedLineDiff(a.rootNode, b.rootNode, cfg, diffLines(oldText), diffLines(newText)), diffLines(oldText).length) : undefined;
|
|
401
|
+
const structure = run('structure', () => detectStructure(a.rootNode, b.rootNode, cfg, keptLines), { operations: [], folds: [], paired: [], wordsChanged: new Set() });
|
|
354
402
|
const textDiff = computeTextDiff(oldText, newText, 0.4 * WORK_PER_SECOND);
|
|
355
403
|
const text = run('text-moves', () => detectTextMoves(textDiff, oldText, newText), { operations: [], folds: [] });
|
|
356
404
|
// A text move is redundant when every non-blank line it moves is already explained.
|
|
@@ -403,6 +451,27 @@ export async function semanticDiffFile(language, oldText, newText, name = 'file'
|
|
|
403
451
|
const textKept = text.operations.map((op, i) => ({ op, i })).filter(({ op }) => !covered(op) && agrees(op));
|
|
404
452
|
// Plain edits go last: they describe only what no semantic pass explained.
|
|
405
453
|
const explainedOps = [...inlined, ...structure.operations, ...textKept.map((t) => t.op), ...reindented(blocks, oldLines, newLines, inString, language)];
|
|
454
|
+
// Trial DROP_UNTOUCHED_MOVE: a structure move none of whose non-blank lines, on either side, is a changed line
|
|
455
|
+
// claims nothing the patch shows (the code only shifted against neighbours that were added or removed, or it
|
|
456
|
+
// sits where it was under a changed container header): drop it, before it takes any line from the passes after it.
|
|
457
|
+
const untouchedMoves = new Set();
|
|
458
|
+
if (trial('DROP_UNTOUCHED_MOVE'))
|
|
459
|
+
structure.operations.forEach((op, k) => {
|
|
460
|
+
if (op.type !== 'moved' && op.type !== 'movedWithEdit')
|
|
461
|
+
return;
|
|
462
|
+
const touched = ['old', 'new'].some((side) => op.lines[side].some((r) => {
|
|
463
|
+
const lines = side === 'old' ? oldLines : newLines;
|
|
464
|
+
for (let l = r.start; l <= r.end; l++)
|
|
465
|
+
if ((lines[l - 1] ?? '').trim() && changed[side].has(l))
|
|
466
|
+
return true;
|
|
467
|
+
return false;
|
|
468
|
+
}));
|
|
469
|
+
if (!touched)
|
|
470
|
+
untouchedMoves.add(inlined.length + k);
|
|
471
|
+
});
|
|
472
|
+
// The dropped move keeps its place in the list (folds refer to operations by index) but owns no line.
|
|
473
|
+
for (const i of untouchedMoves)
|
|
474
|
+
explainedOps[i] = { ...explainedOps[i], lines: { old: [], new: [] } };
|
|
406
475
|
const assigned = assignLines([...explainedOps, ...detectEdits(replacementsFromBlocks(blocks, oldText, newText), oldText, newText)]);
|
|
407
476
|
// Drop operations left with nothing to show: semantic ones that lost every
|
|
408
477
|
// line to an earlier pass (e.g. the same extract found from a second call
|
|
@@ -422,7 +491,11 @@ export async function semanticDiffFile(language, oldText, newText, name = 'file'
|
|
|
422
491
|
if (!keep || (op.type === 'movedWithEdit' && !alike(op, oldLines, newLines)))
|
|
423
492
|
return;
|
|
424
493
|
// A claim the language holds back: the safety net below marks its lines as plain changes.
|
|
425
|
-
if (cfg.holdBack?.has(op.type))
|
|
494
|
+
if (rule('HOLD_BACK') && cfg.holdBack?.has(op.type))
|
|
495
|
+
return;
|
|
496
|
+
// Formatting whose words changed (a docstring reworded, not just rewrapped) is an edit: its lines show as
|
|
497
|
+
// plain changes. Dropped here, not in the structure pass, so no other pass sees a difference.
|
|
498
|
+
if (rule('REFORMAT_WORDS') && i >= inlined.length && structure.wordsChanged.has(i - inlined.length))
|
|
426
499
|
return;
|
|
427
500
|
// Lines that now sit in another context (wrapped into Ruby's `class << self`, under a new `private`) are
|
|
428
501
|
// not just laid out differently either.
|
|
@@ -434,6 +507,10 @@ export async function semanticDiffFile(language, oldText, newText, name = 'file'
|
|
|
434
507
|
finalIndex.set(i, operations.length);
|
|
435
508
|
operations.push(op);
|
|
436
509
|
});
|
|
510
|
+
// Trial ATTACH_BLANK: a blank separator line left beside a moved block (removed with it, or added with it) is part
|
|
511
|
+
// of the move, not a change of its own.
|
|
512
|
+
if (trial('ATTACH_BLANK'))
|
|
513
|
+
attachBlankSeparators(operations, changed, inString, oldLines, newLines);
|
|
437
514
|
operations.push(...uncoveredChanges(blocks, oldText, newText, operations, inString));
|
|
438
515
|
const structureOffset = inlined.length;
|
|
439
516
|
const textOffset = structureOffset + structure.operations.length;
|
|
@@ -450,7 +527,18 @@ export async function semanticDiffFile(language, oldText, newText, name = 'file'
|
|
|
450
527
|
counts: { moved: l.end - l.start + 1, edited: 0, removed: 0 },
|
|
451
528
|
})))
|
|
452
529
|
.sort((x, y) => x.old.start - y.old.start);
|
|
453
|
-
|
|
530
|
+
const semanticOperations = withinFile(operations, dOld.length, dNew.length);
|
|
531
|
+
// The declaration each operation is about (docs/symbols.md). Additive: a failure here only costs the symbols.
|
|
532
|
+
try {
|
|
533
|
+
attachSymbols(semanticOperations, a.rootNode, b.rootNode, oldText, newText, cfg);
|
|
534
|
+
}
|
|
535
|
+
catch (e) {
|
|
536
|
+
if (flags.reasons())
|
|
537
|
+
console.error('[semantic] symbols failed:', e);
|
|
538
|
+
for (const op of semanticOperations)
|
|
539
|
+
delete op.symbol;
|
|
540
|
+
}
|
|
541
|
+
return { semanticOperations, semanticFolds, patch: toPatch(name, blocks, dOld, dNew), diagnostics };
|
|
454
542
|
});
|
|
455
543
|
if (!report)
|
|
456
544
|
return result;
|