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 192 to 193

Compare

+240 −0 in 1 place
new file, 240 lines
1+// A line's earlier changes, for why (abso-core issue 20). A line's id names
2+// the change that wrote it, so a line that was rewritten (reformatted,
3+// renamed, edited) is a new line, and the one it replaced is a deleted line
4+// in the same file. The file's full sequence keeps deleted lines where they
5+// were, so the lines a change deleted and the lines it wrote sit together:
6+// that's one hunk, and the line it replaced is the one it pairs with there.
7+// Following those pairs back gives the line's changes, newest first. A
8+// change that only touched formatting (whitespace, line wrapping, quotes,
9+// trailing commas and semicolons, parentheses around one arrow parameter)
10+// is marked, so why can skip past it to the change that said what the line
11+// does.
13+import type { Atom, AtomId, Graph, LineId, ReadView, SeqLine } from "../../src/engine/index.ts";
15+/** One change in a line's past, newest first. */
16+export interface TrailStep {
17+ /** The change that wrote the line. */
18+ readonly atom: AtomId;
19+ /** The line it wrote, and its text then. */
20+ readonly line: LineId;
21+ readonly text: string;
22+ /** It only changed formatting from the line it replaced. */
23+ readonly formatting: boolean;
24+ /** The line it replaced, when it replaced one. */
25+ readonly replaced?: LineId;
26+}
28+/** At most this many steps back. */
29+export const TRAIL_MAX = 50;
31+/**
32+ * Text with formatting taken out, for comparing two versions of the same
33+ * code: no whitespace, one kind of quote, no trailing commas before a
34+ * closing bracket, no semicolons at line ends (or before a comment there), and no parentheses around a
35+ * lone arrow parameter. Lines are normalized then joined, so a statement
36+ * wrapped onto three lines matches it on one.
37+ */
38+export function normalize(lines: readonly string[]): string {
39+ return lines
40+ .map((l) => l.replace(/;\s*(\/\/.*)?$/, "$1"))
41+ .join("")
42+ .replace(/\s+/g, "")
43+ .replace(/['`]/g, '"')
44+ .replace(/,(?=[)\]}]|$)/g, "")
45+ .replace(/\(([A-Za-z_$][\w$]*)\)=>/g, "$1=>");
46+}
48+/** Whether two runs of lines are the same code, formatted differently (or not at all). */
49+export function sameCode(before: readonly string[], after: readonly string[]): boolean {
50+ return normalize(before) === normalize(after);
51+}
53+/** Similarity of two strings, 0 to 1: shared character pairs (Dice). */
54+function similarity(a: string, b: string): number {
55+ if (a === b) return 1;
56+ if (a.length < 2 || b.length < 2) return 0;
57+ const pairs = new Map<string, number>();
58+ for (let i = 0; i < a.length - 1; i++) {
59+ const p = a.slice(i, i + 2);
60+ pairs.set(p, (pairs.get(p) ?? 0) + 1);
61+ }
62+ let shared = 0;
63+ for (let i = 0; i < b.length - 1; i++) {
64+ const p = b.slice(i, i + 2);
65+ const n = pairs.get(p) ?? 0;
66+ if (n > 0) {
67+ shared++;
68+ pairs.set(p, n - 1);
69+ }
70+ }
71+ return (2 * shared) / (a.length + b.length - 2);
72+}
74+/** Below this, a rewritten line isn't taken for the one it replaced: it was new. */
75+const SIMILAR = 0.4;
77+/**
78+ * For a run that's the same code reformatted: which old line each new line
79+ * came from, by where its characters fall in the joined, normalized text
80+ * (the old line it overlaps most).
81+ */
82+function byCharacters(before: readonly string[], after: readonly string[], at: number): number | undefined {
83+ const spans = (lines: readonly string[]) => {
84+ let from = 0;
85+ return lines.map((l) => {
86+ const n = normalize([l]).length;
87+ const span = [from, from + n] as const;
88+ from += n;
89+ return span;
90+ });
91+ };
92+ const [from, to] = spans(after)[at]!;
93+ let best: number | undefined;
94+ let most = 0;
95+ for (const [i, [a, b]] of spans(before).entries()) {
96+ const overlap = Math.min(to, b) - Math.max(from, a);
97+ if (overlap > most) [best, most] = [i, overlap];
98+ }
99+ return best;
100+}
102+/** Longest common subsequence of two lists of keys: matched index pairs, in order. */
103+function common(a: readonly string[], b: readonly string[]): [number, number][] {
104+ const n = a.length;
105+ const m = b.length;
106+ // Hunks are small; a full table is fine, but cap it so a whole-file rewrite stays cheap.
107+ if (n * m > 4_000_000) return [];
108+ const t: Uint32Array[] = Array.from({ length: n + 1 }, () => new Uint32Array(m + 1));
109+ for (let i = n - 1; i >= 0; i--)
110+ for (let j = m - 1; j >= 0; j--)
111+ t[i]![j] = a[i] === b[j] ? t[i + 1]![j + 1]! + 1 : Math.max(t[i + 1]![j]!, t[i]![j + 1]!);
112+ const out: [number, number][] = [];
113+ let i = 0;
114+ let j = 0;
115+ while (i < n && j < m) {
116+ if (a[i] === b[j]) {
117+ out.push([i, j]);
118+ i++;
119+ j++;
120+ } else if (t[i + 1]![j]! >= t[i]![j + 1]!) i++;
121+ else j++;
122+ }
123+ return out;
124+}
126+/**
127+ * In one hunk, the old line a new one replaced, and whether that was only
128+ * formatting. Lines whose normalized text matches pair first; between those,
129+ * a run that's the same code rewrapped pairs by its characters; anything
130+ * else pairs with the most similar old line, if one is similar enough.
131+ */
132+export function pairIn(
133+ before: readonly string[],
134+ after: readonly string[],
135+ at: number,
136+): { index: number; formatting: boolean } | undefined {
137+ if (before.length === 0) return undefined;
138+ if (sameCode(before, after)) {
139+ const index = byCharacters(before, after, at);
140+ return index === undefined ? undefined : { index, formatting: true };
141+ }
142+ const keys = (lines: readonly string[]) => lines.map((l) => normalize([l]));
143+ const a = keys(before);
144+ const b = keys(after);
145+ const anchors = common(a, b);
146+ const hit = anchors.find(([, j]) => j === at);
147+ if (hit && a[hit[0]] !== "") return { index: hit[0], formatting: true };
148+ // The gap between anchors that holds the new line.
149+ let [i0, j0] = [0, 0];
150+ let [i1, j1] = [before.length, after.length];
151+ for (const [i, j] of anchors) {
152+ if (j < at) [i0, j0] = [i + 1, j + 1];
153+ else if (j > at) {
154+ [i1, j1] = [i, j];
155+ break;
156+ }
157+ }
158+ const gapBefore = before.slice(i0, i1);
159+ const gapAfter = after.slice(j0, j1);
160+ if (gapBefore.length === 0) return undefined;
161+ if (sameCode(gapBefore, gapAfter)) {
162+ const index = byCharacters(gapBefore, gapAfter, at - j0);
163+ return index === undefined ? undefined : { index: i0 + index, formatting: true };
164+ }
165+ let best: number | undefined;
166+ let most = SIMILAR;
167+ for (let i = i0; i < i1; i++) {
168+ const s = similarity(a[i]!, b[at]!);
169+ if (s >= most) [best, most] = [i, s];
170+ }
171+ return best === undefined ? undefined : { index: best, formatting: false };
172+}
174+/**
175+ * The line's changes, newest first: the change that wrote it, then the one
176+ * that wrote the line it replaced, and so on back to a change that wrote it
177+ * new. At most `max` steps; `more` says the trail went on.
178+ */
179+export function trail(
180+ graph: Graph,
181+ view: ReadView,
182+ file: string,
183+ line: LineId,
184+ max = TRAIL_MAX,
185+): { steps: TrailStep[]; more: boolean } {
186+ const seq = view.sequence(file);
187+ const index = new Map<LineId, number>();
188+ for (const [i, s] of seq.entries()) index.set(s.id, i);
189+ const steps: TrailStep[] = [];
190+ let current: LineId | undefined = line;
191+ while (current !== undefined && steps.length < max) {
192+ const at = index.get(current);
193+ if (at === undefined) break;
194+ const atom = graph.atom(seq[at]!.atom);
195+ const hunk = hunkOf(graph, seq, at, atom);
196+ const pair = pairIn(
197+ hunk.before.map((i) => seq[i]!.text),
198+ hunk.after.map((i) => seq[i]!.text),
199+ hunk.after.indexOf(at),
200+ );
201+ const replaced = pair ? seq[hunk.before[pair.index]!]!.id : undefined;
202+ steps.push({
203+ atom: atom.id,
204+ line: current,
205+ text: seq[at]!.text,
206+ formatting: pair?.formatting ?? false,
207+ ...(replaced !== undefined ? { replaced } : {}),
208+ });
209+ current = replaced;
210+ }
211+ return { steps, more: current !== undefined };
212+}
214+/**
215+ * The hunk a change made around one of its lines, as indexes into the
216+ * sequence in file order: the lines it deleted there and the lines it wrote.
217+ * Lines nobody could see when it was made (written later, or already
218+ * deleted) are passed over; a line it left alone ends the hunk.
219+ */
220+function hunkOf(graph: Graph, seq: readonly SeqLine[], at: number, atom: Atom): { before: number[]; after: number[] } {
221+ const seqOf = (id: AtomId) => graph.atom(id).seq;
222+ const role = (s: SeqLine): "wrote" | "deleted" | "unseen" | "kept" => {
223+ if (s.atom === atom.id) return "wrote";
224+ if (seqOf(s.atom) > atom.seq) return "unseen";
225+ if (s.deletedBy.some((d) => d !== atom.id && seqOf(d) < atom.seq)) return "unseen";
226+ return s.deletedBy.includes(atom.id) ? "deleted" : "kept";
227+ };
228+ let first = at;
229+ while (first > 0 && role(seq[first - 1]!) !== "kept") first--;
230+ let last = at;
231+ while (last < seq.length - 1 && role(seq[last + 1]!) !== "kept") last++;
232+ const before: number[] = [];
233+ const after: number[] = [];
234+ for (let i = first; i <= last; i++) {
235+ const r = role(seq[i]!);
236+ if (r === "deleted") before.push(i);
237+ else if (r === "wrote") after.push(i);
238+ }
239+ return { before, after };
240+}
MCP
compare on this site's /mcp
CLI
abso diff 192 193 worker/project/trail.ts --json
JSON
/christopher/abso-core/compare/192..193?file=worker%2Fproject%2Ftrail.ts&format=json