Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed

Versions 163 to 318

Compare

+123 −6 in 3 places
Lines 43–171 at 318 (were 43–54 at 163)
4343 * file's length times MAX_COST instead of its length squared.
4444 */
4545export function diffLines(a: readonly string[], b: readonly string[]): Hunk[] {
46− const matches = commonLines(a, b);
47− const hunks: Hunk[] = [];
46+ return hunksFromMatches(a.length, b, commonLines(a, b));
47+}
49+/** Diff a single-pass old-line source without retaining its strings. */
50+export function diffLineStream(a: Iterable<string>, b: readonly string[]): Hunk[] {
51+ const ids = new Map<string, number>();
52+ const next = b.map((line) => {
53+ let id = ids.get(line);
54+ if (id === undefined) {
55+ id = ids.size;
56+ ids.set(line, id);
57+ }
58+ return id;
59+ });
60+ const previous: number[] = [];
61+ for (const line of a) previous.push(ids.get(line) ?? -1);
62+ return hunksFromMatches(previous.length, b, commonLines(previous, next));
63+}
65+/** Compare replayable old-line chunks without assembling historical strings. */
66+export function diffChunkedLines(a: Iterable<() => Iterable<string>>, b: readonly string[]): Hunk[] {
67+ const keys = new Map<string, number>();
68+ const buckets = new Map<string, number[]>();
69+ const next = b.map((line, index) => {
70+ let id = keys.get(line);
71+ if (id === undefined) {
72+ id = index;
73+ keys.set(line, id);
74+ const key = chunkKey([line]);
75+ const bucket = buckets.get(key) ?? [];
76+ bucket.push(id);
77+ buckets.set(key, bucket);
78+ }
79+ return id;
80+ });
81+ const previous: number[] = [];
82+ for (const chunks of a) {
83+ const iterator = chunks()[Symbol.iterator]();
84+ let key: string;
85+ const aligned = b[previous.length];
86+ let alignedOffset = 0;
87+ let alignedEqual = aligned !== undefined;
88+ try {
89+ const first = iterator.next();
90+ if (first.done) {
91+ previous.push(keys.get("") ?? -1);
92+ continue;
93+ }
94+ const second = iterator.next();
95+ if (second.done) {
96+ previous.push(keys.get(first.value) ?? -1);
97+ continue;
98+ }
99+ key = chunkKey(
100+ (function* () {
101+ function* remaining() {
102+ yield first.value;
103+ yield second.value;
104+ for (let next = iterator.next(); !next.done; next = iterator.next()) yield next.value;
105+ }
106+ for (const chunk of remaining()) {
107+ if (alignedEqual && !aligned!.startsWith(chunk, alignedOffset)) alignedEqual = false;
108+ alignedOffset += chunk.length;
109+ yield chunk;
110+ }
111+ })(),
112+ );
113+ } finally {
114+ iterator.return?.();
115+ }
116+ // Same-position unchanged lines are common. Confirm them during hashing,
117+ // retaining exact comparison and the collision-safe fallback for moved lines.
118+ if (alignedEqual && alignedOffset === aligned!.length) {
119+ previous.push(next[previous.length]!);
120+ continue;
121+ }
122+ const candidates = buckets.get(key);
123+ let match = -1;
124+ for (const candidate of candidates ?? []) {
125+ let offset = 0;
126+ let equal = true;
127+ for (const chunk of chunks()) {
128+ if (!b[candidate]!.startsWith(chunk, offset)) equal = false;
129+ offset += chunk.length;
130+ }
131+ if (equal && offset === b[candidate]!.length) {
132+ match = candidate;
133+ break;
134+ }
135+ }
136+ previous.push(match);
137+ }
138+ return hunksFromMatches(previous.length, b, commonLines(previous, next));
139+}
141+function chunkKey(chunks: Iterable<string>): string {
142+ let hash = 2166136261;
143+ let length = 0;
144+ for (const chunk of chunks) {
145+ length += chunk.length;
146+ for (let i = 0; i < chunk.length; i++) hash = Math.imul(hash ^ chunk.charCodeAt(i), 16777619) >>> 0;
147+ }
148+ return `${length}:${hash}`;
149+}
151+/** The existing diff algorithm over exact line symbols, without retaining text. */
152+export function diffLineSymbols(
153+ a: readonly number[],
154+ b: readonly number[],
155+): { start: number; end: number; lines: number[] }[] {
156+ return hunksFromMatches(a.length, b, commonLines(a, b));
157+}
159+function hunksFromMatches<T>(
160+ length: number,
161+ b: readonly T[],
162+ matches: readonly number[],
163+): { start: number; end: number; lines: T[] }[] {
164+ const hunks: { start: number; end: number; lines: T[] }[] = [];
48165 let i = 0;
49166 let j = 0;
50167 for (let m = 0; m <= matches.length; m += 2) {
51− const mi = m < matches.length ? matches[m]! : a.length;
168+ const mi = m < matches.length ? matches[m]! : length;
52169 const mj = m < matches.length ? matches[m + 1]! : b.length;
53170 if (mi > i || mj > j) hunks.push({ start: i, end: mi, lines: b.slice(j, mj) });
54171 i = mi + 1;
Lines 178–184 at 318 (were 61–67 at 163)
61178 * The matched pairs of a longest common subsequence of `a` and `b`, flattened
62179 * as [i0, j0, i1, j1, ...] in increasing order.
63180 */
64−function commonLines(a: readonly string[], b: readonly string[]): number[] {
181+function commonLines<T>(a: readonly T[], b: readonly T[]): number[] {
65182 // Common prefix and suffix match directly.
66183 let pre = 0;
67184 while (pre < a.length && pre < b.length && a[pre] === b[pre]) pre++;
Lines 189–196 at 318 (were 72–79 at 163)
72189
73190 // A line that occurs on only one side can never be matched; leave it out of
74191 // the search (this also makes wholly different texts instant).
75− const ids = new Map<string, number>();
76− const intern = (s: string): number => {
192+ const ids = new Map<T, number>();
193+ const intern = (s: T): number => {
77194 let id = ids.get(s);
78195 if (id === undefined) {
79196 id = ids.size;
MCP
compare on this site's /mcp
CLI
abso diff 163 318 src/engine/text.ts --json
JSON
/christopher/abso-core/compare/163..318?file=src%2Fengine%2Ftext.ts&format=json