autonomous-flow-daemon 1.6.0 → 1.9.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.
- package/CHANGELOG.md +85 -85
- package/LICENSE +21 -21
- package/README-ko.md +282 -0
- package/README.md +282 -266
- package/mcp-config.json +10 -10
- package/package.json +4 -2
- package/src/adapters/index.ts +370 -370
- package/src/cli.ts +162 -127
- package/src/commands/benchmark.ts +187 -187
- package/src/commands/correlate.ts +180 -0
- package/src/commands/dashboard.ts +404 -0
- package/src/commands/evolution.ts +84 -1
- package/src/commands/fix.ts +158 -158
- package/src/commands/lang.ts +41 -41
- package/src/commands/plugin.ts +110 -0
- package/src/commands/restart.ts +14 -14
- package/src/commands/score.ts +276 -276
- package/src/commands/start.ts +155 -155
- package/src/commands/status.ts +157 -157
- package/src/commands/stop.ts +68 -68
- package/src/commands/suggest.ts +211 -0
- package/src/commands/sync.ts +329 -16
- package/src/constants.ts +32 -32
- package/src/core/boast.ts +280 -280
- package/src/core/config.ts +49 -49
- package/src/core/correlation-engine.ts +265 -0
- package/src/core/db.ts +145 -117
- package/src/core/discovery.ts +65 -65
- package/src/core/federation.ts +129 -0
- package/src/core/hologram/engine.ts +71 -71
- package/src/core/hologram/fallback.ts +11 -11
- package/src/core/hologram/go-extractor.ts +203 -0
- package/src/core/hologram/incremental.ts +227 -227
- package/src/core/hologram/py-extractor.ts +132 -132
- package/src/core/hologram/rust-extractor.ts +244 -0
- package/src/core/hologram/ts-extractor.ts +406 -320
- package/src/core/hologram/types.ts +27 -25
- package/src/core/hologram.ts +73 -71
- package/src/core/i18n/messages.ts +309 -309
- package/src/core/locale.ts +88 -88
- package/src/core/log-rotate.ts +33 -33
- package/src/core/log-utils.ts +38 -38
- package/src/core/lru-map.ts +61 -61
- package/src/core/notify.ts +74 -74
- package/src/core/plugin-manager.ts +225 -0
- package/src/core/rule-suggestion.ts +127 -0
- package/src/core/validator-generator.ts +224 -0
- package/src/core/workspace.ts +28 -28
- package/src/daemon/client.ts +78 -65
- package/src/daemon/event-batcher.ts +108 -108
- package/src/daemon/guards.ts +13 -13
- package/src/daemon/http-routes.ts +376 -293
- package/src/daemon/mcp-handler.ts +575 -270
- package/src/daemon/mcp-subscriptions.ts +81 -0
- package/src/daemon/mesh.ts +51 -0
- package/src/daemon/server.ts +655 -590
- package/src/daemon/types.ts +121 -100
- package/src/daemon/workspace-map.ts +104 -92
- package/src/platform.ts +60 -60
- package/src/version.ts +15 -15
- package/README.ko.md +0 -266
|
@@ -1,227 +1,227 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Incremental Hologram — diff-only mode.
|
|
3
|
-
*
|
|
4
|
-
* Compares previous and current hologram extractions,
|
|
5
|
-
* returns only changed nodes with surrounding context in unified-diff style.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import type { Tree } from "web-tree-sitter";
|
|
9
|
-
import type { HologramResult, LanguageExtractor, HologramOptions } from "./types";
|
|
10
|
-
import { TreeSitterEngine } from "./engine";
|
|
11
|
-
|
|
12
|
-
/** In-memory cache for previous hologram lines (per file path) */
|
|
13
|
-
const hologramCache = new Map<string, string[]>();
|
|
14
|
-
|
|
15
|
-
/** Maximum cache entries */
|
|
16
|
-
const MAX_CACHE_SIZE = 200;
|
|
17
|
-
|
|
18
|
-
export function clearHologramCache(): void {
|
|
19
|
-
hologramCache.clear();
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export function setCachedHologram(filePath: string, lines: string[]): void {
|
|
23
|
-
// True LRU: delete existing entry first so re-insert moves it to end
|
|
24
|
-
hologramCache.delete(filePath);
|
|
25
|
-
if (hologramCache.size >= MAX_CACHE_SIZE) {
|
|
26
|
-
const oldestKey = hologramCache.keys().next().value;
|
|
27
|
-
if (oldestKey) hologramCache.delete(oldestKey);
|
|
28
|
-
}
|
|
29
|
-
hologramCache.set(filePath, lines);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export function getCachedHologram(filePath: string): string[] | undefined {
|
|
33
|
-
const value = hologramCache.get(filePath);
|
|
34
|
-
if (value !== undefined) {
|
|
35
|
-
// Promote to most-recently-used position
|
|
36
|
-
hologramCache.delete(filePath);
|
|
37
|
-
hologramCache.set(filePath, value);
|
|
38
|
-
}
|
|
39
|
-
return value;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Generate an incremental (diff-only) hologram.
|
|
44
|
-
* Compares current extraction with cached previous result.
|
|
45
|
-
* Returns unified-diff style output showing only changed nodes.
|
|
46
|
-
*/
|
|
47
|
-
export async function generateIncrementalHologram(
|
|
48
|
-
filePath: string,
|
|
49
|
-
source: string,
|
|
50
|
-
extractor: LanguageExtractor,
|
|
51
|
-
options?: HologramOptions,
|
|
52
|
-
): Promise<HologramResult> {
|
|
53
|
-
const engine = await TreeSitterEngine.getInstance();
|
|
54
|
-
const tree = await engine.parse(source, extractor.grammarName);
|
|
55
|
-
const currentLines = extractor.extract(tree, source, options);
|
|
56
|
-
tree.delete();
|
|
57
|
-
|
|
58
|
-
const previousLines = getCachedHologram(filePath);
|
|
59
|
-
|
|
60
|
-
// Cache current result for next diff
|
|
61
|
-
setCachedHologram(filePath, currentLines);
|
|
62
|
-
|
|
63
|
-
// No previous → return full hologram with diff header
|
|
64
|
-
if (!previousLines) {
|
|
65
|
-
const hologram = currentLines.join("\n");
|
|
66
|
-
return {
|
|
67
|
-
hologram,
|
|
68
|
-
originalLength: source.length,
|
|
69
|
-
hologramLength: hologram.length,
|
|
70
|
-
savings: source.length > 0
|
|
71
|
-
? Math.round((source.length - hologram.length) / source.length * 1000) / 10
|
|
72
|
-
: 0,
|
|
73
|
-
language: extractor.grammarName,
|
|
74
|
-
isDiff: false,
|
|
75
|
-
changedNodes: currentLines.length,
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// Diff previous vs current lines
|
|
80
|
-
const diffOutput = buildUnifiedDiff(filePath, previousLines, currentLines);
|
|
81
|
-
|
|
82
|
-
return {
|
|
83
|
-
hologram: diffOutput.text,
|
|
84
|
-
originalLength: source.length,
|
|
85
|
-
hologramLength: diffOutput.text.length,
|
|
86
|
-
savings: source.length > 0
|
|
87
|
-
? Math.round((source.length - diffOutput.text.length) / source.length * 1000) / 10
|
|
88
|
-
: 0,
|
|
89
|
-
language: extractor.grammarName,
|
|
90
|
-
isDiff: true,
|
|
91
|
-
changedNodes: diffOutput.changedCount,
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
interface DiffOutput {
|
|
96
|
-
text: string;
|
|
97
|
-
changedCount: number;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Build a unified-diff style output comparing old and new hologram lines.
|
|
102
|
-
* Groups unchanged lines into summary markers, shows changed lines with +/- prefixes.
|
|
103
|
-
*/
|
|
104
|
-
function buildUnifiedDiff(filePath: string, oldLines: string[], newLines: string[]): DiffOutput {
|
|
105
|
-
const header = `--- a/${filePath} (previous)\n+++ b/${filePath} (current)\n`;
|
|
106
|
-
|
|
107
|
-
// Simple line-by-line diff using LCS approach
|
|
108
|
-
const hunks = computeHunks(oldLines, newLines);
|
|
109
|
-
|
|
110
|
-
if (hunks.length === 0) {
|
|
111
|
-
return {
|
|
112
|
-
text: header + "@@ no changes @@",
|
|
113
|
-
changedCount: 0,
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
const parts: string[] = [header];
|
|
118
|
-
let changedCount = 0;
|
|
119
|
-
let oldIdx = 0;
|
|
120
|
-
|
|
121
|
-
for (const hunk of hunks) {
|
|
122
|
-
// Show unchanged lines before this hunk as a summary
|
|
123
|
-
const unchangedBefore = hunk.oldStart - oldIdx;
|
|
124
|
-
if (unchangedBefore > 0) {
|
|
125
|
-
const summaryLines = oldLines.slice(oldIdx, hunk.oldStart);
|
|
126
|
-
const firstLine = summaryLines[0]?.split("{")[0]?.trim() ?? "...";
|
|
127
|
-
parts.push(`@@ unchanged: ${firstLine} (${unchangedBefore} ${unchangedBefore === 1 ? "declaration" : "declarations"}) @@`);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// Show removed lines
|
|
131
|
-
for (let i = hunk.oldStart; i < hunk.oldStart + hunk.oldCount; i++) {
|
|
132
|
-
parts.push(`- ${oldLines[i]}`);
|
|
133
|
-
changedCount++;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// Show added lines
|
|
137
|
-
for (let i = hunk.newStart; i < hunk.newStart + hunk.newCount; i++) {
|
|
138
|
-
parts.push(`+ ${newLines[i]}`);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
oldIdx = hunk.oldStart + hunk.oldCount;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
// Trailing unchanged
|
|
145
|
-
const trailingCount = oldLines.length - oldIdx;
|
|
146
|
-
if (trailingCount > 0) {
|
|
147
|
-
parts.push(`@@ unchanged: ${trailingCount} more ${trailingCount === 1 ? "declaration" : "declarations"} @@`);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
return { text: parts.join("\n"), changedCount };
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
interface Hunk {
|
|
154
|
-
oldStart: number;
|
|
155
|
-
oldCount: number;
|
|
156
|
-
newStart: number;
|
|
157
|
-
newCount: number;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* Compute diff hunks between old and new line arrays.
|
|
162
|
-
* Uses a simple O(n*m) LCS-based diff suitable for small arrays (hologram lines are typically < 100).
|
|
163
|
-
*/
|
|
164
|
-
function computeHunks(oldLines: string[], newLines: string[]): Hunk[] {
|
|
165
|
-
const n = oldLines.length;
|
|
166
|
-
const m = newLines.length;
|
|
167
|
-
|
|
168
|
-
// Guard: for very large inputs, fall back to full diff to stay within SEAM budget
|
|
169
|
-
if (n * m > 50_000) {
|
|
170
|
-
return [{ oldStart: 0, oldCount: n, newStart: 0, newCount: m }];
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
// Build LCS table
|
|
174
|
-
const dp: number[][] = Array.from({ length: n + 1 }, () => Array(m + 1).fill(0));
|
|
175
|
-
for (let i = 1; i <= n; i++) {
|
|
176
|
-
for (let j = 1; j <= m; j++) {
|
|
177
|
-
if (oldLines[i - 1] === newLines[j - 1]) {
|
|
178
|
-
dp[i][j] = dp[i - 1][j - 1] + 1;
|
|
179
|
-
} else {
|
|
180
|
-
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
// Backtrack to find matching lines
|
|
186
|
-
const matches: Array<[number, number]> = [];
|
|
187
|
-
let i = n, j = m;
|
|
188
|
-
while (i > 0 && j > 0) {
|
|
189
|
-
if (oldLines[i - 1] === newLines[j - 1]) {
|
|
190
|
-
matches.unshift([i - 1, j - 1]);
|
|
191
|
-
i--; j--;
|
|
192
|
-
} else if (dp[i - 1][j] > dp[i][j - 1]) {
|
|
193
|
-
i--;
|
|
194
|
-
} else {
|
|
195
|
-
j--;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
// Convert matches to hunks (gaps between matches)
|
|
200
|
-
const hunks: Hunk[] = [];
|
|
201
|
-
let oi = 0, ni = 0;
|
|
202
|
-
|
|
203
|
-
for (const [mi, mj] of matches) {
|
|
204
|
-
if (oi < mi || ni < mj) {
|
|
205
|
-
hunks.push({
|
|
206
|
-
oldStart: oi,
|
|
207
|
-
oldCount: mi - oi,
|
|
208
|
-
newStart: ni,
|
|
209
|
-
newCount: mj - ni,
|
|
210
|
-
});
|
|
211
|
-
}
|
|
212
|
-
oi = mi + 1;
|
|
213
|
-
ni = mj + 1;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
// Trailing diff
|
|
217
|
-
if (oi < n || ni < m) {
|
|
218
|
-
hunks.push({
|
|
219
|
-
oldStart: oi,
|
|
220
|
-
oldCount: n - oi,
|
|
221
|
-
newStart: ni,
|
|
222
|
-
newCount: m - ni,
|
|
223
|
-
});
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
return hunks;
|
|
227
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Incremental Hologram — diff-only mode.
|
|
3
|
+
*
|
|
4
|
+
* Compares previous and current hologram extractions,
|
|
5
|
+
* returns only changed nodes with surrounding context in unified-diff style.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { Tree } from "web-tree-sitter";
|
|
9
|
+
import type { HologramResult, LanguageExtractor, HologramOptions } from "./types";
|
|
10
|
+
import { TreeSitterEngine } from "./engine";
|
|
11
|
+
|
|
12
|
+
/** In-memory cache for previous hologram lines (per file path) */
|
|
13
|
+
const hologramCache = new Map<string, string[]>();
|
|
14
|
+
|
|
15
|
+
/** Maximum cache entries */
|
|
16
|
+
const MAX_CACHE_SIZE = 200;
|
|
17
|
+
|
|
18
|
+
export function clearHologramCache(): void {
|
|
19
|
+
hologramCache.clear();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function setCachedHologram(filePath: string, lines: string[]): void {
|
|
23
|
+
// True LRU: delete existing entry first so re-insert moves it to end
|
|
24
|
+
hologramCache.delete(filePath);
|
|
25
|
+
if (hologramCache.size >= MAX_CACHE_SIZE) {
|
|
26
|
+
const oldestKey = hologramCache.keys().next().value;
|
|
27
|
+
if (oldestKey) hologramCache.delete(oldestKey);
|
|
28
|
+
}
|
|
29
|
+
hologramCache.set(filePath, lines);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function getCachedHologram(filePath: string): string[] | undefined {
|
|
33
|
+
const value = hologramCache.get(filePath);
|
|
34
|
+
if (value !== undefined) {
|
|
35
|
+
// Promote to most-recently-used position
|
|
36
|
+
hologramCache.delete(filePath);
|
|
37
|
+
hologramCache.set(filePath, value);
|
|
38
|
+
}
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Generate an incremental (diff-only) hologram.
|
|
44
|
+
* Compares current extraction with cached previous result.
|
|
45
|
+
* Returns unified-diff style output showing only changed nodes.
|
|
46
|
+
*/
|
|
47
|
+
export async function generateIncrementalHologram(
|
|
48
|
+
filePath: string,
|
|
49
|
+
source: string,
|
|
50
|
+
extractor: LanguageExtractor,
|
|
51
|
+
options?: HologramOptions,
|
|
52
|
+
): Promise<HologramResult> {
|
|
53
|
+
const engine = await TreeSitterEngine.getInstance();
|
|
54
|
+
const tree = await engine.parse(source, extractor.grammarName);
|
|
55
|
+
const currentLines = extractor.extract(tree, source, options);
|
|
56
|
+
tree.delete();
|
|
57
|
+
|
|
58
|
+
const previousLines = getCachedHologram(filePath);
|
|
59
|
+
|
|
60
|
+
// Cache current result for next diff
|
|
61
|
+
setCachedHologram(filePath, currentLines);
|
|
62
|
+
|
|
63
|
+
// No previous → return full hologram with diff header
|
|
64
|
+
if (!previousLines) {
|
|
65
|
+
const hologram = currentLines.join("\n");
|
|
66
|
+
return {
|
|
67
|
+
hologram,
|
|
68
|
+
originalLength: source.length,
|
|
69
|
+
hologramLength: hologram.length,
|
|
70
|
+
savings: source.length > 0
|
|
71
|
+
? Math.round((source.length - hologram.length) / source.length * 1000) / 10
|
|
72
|
+
: 0,
|
|
73
|
+
language: extractor.grammarName,
|
|
74
|
+
isDiff: false,
|
|
75
|
+
changedNodes: currentLines.length,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Diff previous vs current lines
|
|
80
|
+
const diffOutput = buildUnifiedDiff(filePath, previousLines, currentLines);
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
hologram: diffOutput.text,
|
|
84
|
+
originalLength: source.length,
|
|
85
|
+
hologramLength: diffOutput.text.length,
|
|
86
|
+
savings: source.length > 0
|
|
87
|
+
? Math.round((source.length - diffOutput.text.length) / source.length * 1000) / 10
|
|
88
|
+
: 0,
|
|
89
|
+
language: extractor.grammarName,
|
|
90
|
+
isDiff: true,
|
|
91
|
+
changedNodes: diffOutput.changedCount,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
interface DiffOutput {
|
|
96
|
+
text: string;
|
|
97
|
+
changedCount: number;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Build a unified-diff style output comparing old and new hologram lines.
|
|
102
|
+
* Groups unchanged lines into summary markers, shows changed lines with +/- prefixes.
|
|
103
|
+
*/
|
|
104
|
+
function buildUnifiedDiff(filePath: string, oldLines: string[], newLines: string[]): DiffOutput {
|
|
105
|
+
const header = `--- a/${filePath} (previous)\n+++ b/${filePath} (current)\n`;
|
|
106
|
+
|
|
107
|
+
// Simple line-by-line diff using LCS approach
|
|
108
|
+
const hunks = computeHunks(oldLines, newLines);
|
|
109
|
+
|
|
110
|
+
if (hunks.length === 0) {
|
|
111
|
+
return {
|
|
112
|
+
text: header + "@@ no changes @@",
|
|
113
|
+
changedCount: 0,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const parts: string[] = [header];
|
|
118
|
+
let changedCount = 0;
|
|
119
|
+
let oldIdx = 0;
|
|
120
|
+
|
|
121
|
+
for (const hunk of hunks) {
|
|
122
|
+
// Show unchanged lines before this hunk as a summary
|
|
123
|
+
const unchangedBefore = hunk.oldStart - oldIdx;
|
|
124
|
+
if (unchangedBefore > 0) {
|
|
125
|
+
const summaryLines = oldLines.slice(oldIdx, hunk.oldStart);
|
|
126
|
+
const firstLine = summaryLines[0]?.split("{")[0]?.trim() ?? "...";
|
|
127
|
+
parts.push(`@@ unchanged: ${firstLine} (${unchangedBefore} ${unchangedBefore === 1 ? "declaration" : "declarations"}) @@`);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Show removed lines
|
|
131
|
+
for (let i = hunk.oldStart; i < hunk.oldStart + hunk.oldCount; i++) {
|
|
132
|
+
parts.push(`- ${oldLines[i]}`);
|
|
133
|
+
changedCount++;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Show added lines
|
|
137
|
+
for (let i = hunk.newStart; i < hunk.newStart + hunk.newCount; i++) {
|
|
138
|
+
parts.push(`+ ${newLines[i]}`);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
oldIdx = hunk.oldStart + hunk.oldCount;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Trailing unchanged
|
|
145
|
+
const trailingCount = oldLines.length - oldIdx;
|
|
146
|
+
if (trailingCount > 0) {
|
|
147
|
+
parts.push(`@@ unchanged: ${trailingCount} more ${trailingCount === 1 ? "declaration" : "declarations"} @@`);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return { text: parts.join("\n"), changedCount };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
interface Hunk {
|
|
154
|
+
oldStart: number;
|
|
155
|
+
oldCount: number;
|
|
156
|
+
newStart: number;
|
|
157
|
+
newCount: number;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Compute diff hunks between old and new line arrays.
|
|
162
|
+
* Uses a simple O(n*m) LCS-based diff suitable for small arrays (hologram lines are typically < 100).
|
|
163
|
+
*/
|
|
164
|
+
function computeHunks(oldLines: string[], newLines: string[]): Hunk[] {
|
|
165
|
+
const n = oldLines.length;
|
|
166
|
+
const m = newLines.length;
|
|
167
|
+
|
|
168
|
+
// Guard: for very large inputs, fall back to full diff to stay within SEAM budget
|
|
169
|
+
if (n * m > 50_000) {
|
|
170
|
+
return [{ oldStart: 0, oldCount: n, newStart: 0, newCount: m }];
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Build LCS table
|
|
174
|
+
const dp: number[][] = Array.from({ length: n + 1 }, () => Array(m + 1).fill(0));
|
|
175
|
+
for (let i = 1; i <= n; i++) {
|
|
176
|
+
for (let j = 1; j <= m; j++) {
|
|
177
|
+
if (oldLines[i - 1] === newLines[j - 1]) {
|
|
178
|
+
dp[i][j] = dp[i - 1][j - 1] + 1;
|
|
179
|
+
} else {
|
|
180
|
+
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Backtrack to find matching lines
|
|
186
|
+
const matches: Array<[number, number]> = [];
|
|
187
|
+
let i = n, j = m;
|
|
188
|
+
while (i > 0 && j > 0) {
|
|
189
|
+
if (oldLines[i - 1] === newLines[j - 1]) {
|
|
190
|
+
matches.unshift([i - 1, j - 1]);
|
|
191
|
+
i--; j--;
|
|
192
|
+
} else if (dp[i - 1][j] > dp[i][j - 1]) {
|
|
193
|
+
i--;
|
|
194
|
+
} else {
|
|
195
|
+
j--;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Convert matches to hunks (gaps between matches)
|
|
200
|
+
const hunks: Hunk[] = [];
|
|
201
|
+
let oi = 0, ni = 0;
|
|
202
|
+
|
|
203
|
+
for (const [mi, mj] of matches) {
|
|
204
|
+
if (oi < mi || ni < mj) {
|
|
205
|
+
hunks.push({
|
|
206
|
+
oldStart: oi,
|
|
207
|
+
oldCount: mi - oi,
|
|
208
|
+
newStart: ni,
|
|
209
|
+
newCount: mj - ni,
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
oi = mi + 1;
|
|
213
|
+
ni = mj + 1;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Trailing diff
|
|
217
|
+
if (oi < n || ni < m) {
|
|
218
|
+
hunks.push({
|
|
219
|
+
oldStart: oi,
|
|
220
|
+
oldCount: n - oi,
|
|
221
|
+
newStart: ni,
|
|
222
|
+
newCount: m - ni,
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return hunks;
|
|
227
|
+
}
|