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
MCP
show on this site's /mcp
CLI
abso show 194 --json
JSON
/christopher/abso-core/versions/194?format=json

abso why follows code moved between files, and why's text for scripts

of shared·whytrail··Not checked

A line that a push wrote new while taking the same code out of another file (a file split in two, code moved and reindented) now leads back into the file it came from, and why counts that push as a move: 'Skipped 1 formatting change and 1 move since: ..., moved from src/old.ts'. Code moved as it is already kept its lines and their history. Short lines, like a brace, aren't followed: they match too much.

whyText (client/src/history.ts) prints an answer as abso why does, for scripts that read an answer some other way. Long first lines of a push's message are cut to 100 characters in the skip note.

  • amended; first pushed as: abso why follows code moved between files, and why's text for scripts A line that a push wrote new while taking the same code out of another file (a file split in two, code moved and reindented) now leads back into the file it came from, and why counts that push as a move: 'Skipped 1 formatting change and 1 move since: version 430, moved from worker/project/project.ts; ...'. Code moved as it is already kept its lines and their history. Short lines, like a brace, aren't followed: they match too much. whyText (client/src/history.ts) prints an answer as abso why does, for scripts that read an answer some other way. Long first lines of a push's message are cut to 100 characters in the skip note.

Version 193 · Files at version 194

5 files, 219 lines added, 50 removed.

--- a/client/src/history-answers.ts
+++ b/client/src/history-answers.ts
@@ -166,7 +166,10 @@
166166 readonly message: string;
167167 readonly time: number;
168168 readonly text: string;
169+ /** It only changed formatting, or only moved the line. */
169170 readonly formatting: boolean;
171+ /** The file it moved the line from, when another. */
172+ readonly movedFrom?: string;
170173 readonly publish: Publish | null;
171174}
172175export interface WhyAnswer {
--- a/client/src/history.ts
+++ b/client/src/history.ts
@@ -9,7 +9,7 @@
99import { findRoot, readConfig } from "./config.ts";
1010import { relativeTo } from "./folder.ts";
1111import { type GitOrigin, gitOrigin, issueLinks } from "./git-origin.ts";
12−import { byWhom, type TrailStep } from "./history-answers.ts";
12+import { byWhom, type TrailStep, type WhyAnswer } from "./history-answers.ts";
1313
1414function open(env: Env): { root: string; api: Api; agent: string } {
1515 const root = findRoot(env.cwd);
@@ -61,9 +61,20 @@
6161 return [` From git: commit ${o.commit}${o.author ? ` by ${o.author}` : ""}${o.time ? `, ${o.time}` : ""}.`];
6262}
6363
64+/** "1 formatting change and 2 moves": what why skipped. */
65+function skippedKinds(steps: readonly TrailStep[]): string {
66+ const moves = steps.filter((s) => s.movedFrom !== undefined).length;
67+ const formatting = steps.length - moves;
68+ return [
69+ ...(formatting > 0 ? [`${formatting} formatting change${formatting === 1 ? "" : "s"}`] : []),
70+ ...(moves > 0 ? [`${moves} move${moves === 1 ? "" : "s"}`] : []),
71+ ].join(" and ");
72+}
6474/** A trail step in a few words: its version (or that it isn't pushed), who, and the first line of why. */
6575function stepName(s: TrailStep, long = false): string {
66− const subject = (s.publish ? (gitOrigin(s.publish)?.message ?? s.publish.message) : s.message).split("\n")[0]!;
76+ const first = (s.publish ? (gitOrigin(s.publish)?.message ?? s.publish.message) : s.message).split("\n")[0]!;
77+ const subject = first.length > 100 ? `${first.slice(0, 97).trimEnd()}...` : first;
6778 const where = s.publish === null ? "not pushed" : `version ${s.publish.version}`;
6879 const who = s.publish ? byWhom(s.publish) : s.author;
6980 return long
@@ -147,8 +158,15 @@
147158 const w = await api.why(file, lineNumber(raw, "the line")!, view, at);
148159 if (at !== undefined && w.version !== at)
149160 throw new UsageError(`this server doesn't read why at a version yet; abso blame ${w.file} --version ${at} does`);
150− // The line's changes (abso-core issue 20): the newest one that changed what it does leads, past formatting-only ones.
151− const all = bool(args, "all");
161+ return { text: whyText(w, { at, all: bool(args, "all") }), json: { ...w } };
162+}
164+/**
165+ * What abso why prints for an answer. The line's changes (abso-core issue
166+ * 20): the newest one that changed what it does leads, past formatting-only
167+ * ones, unless `all`.
168+ */
169+export function whyText(w: WhyAnswer, { at, all = false }: { at?: number; all?: boolean } = {}): string {
152170 const trail = w.trail ?? [];
153171 const lead = (all ? trail[0] : trail.find((s) => !s.formatting)) ?? trail[0];
154172 const leadAt = lead === undefined ? 0 : trail.indexOf(lead);
@@ -161,13 +179,13 @@
161179 ? [`Written by ${w.change.author} (${when(w.change.time)}), pushed in ${pushed(w.publish, w.gitIssues)}.`]
162180 : [
163181 `Written by ${lead.author} (${when(lead.time)}), pushed in ${pushed(lead.publish, w.gitIssues)}.`,
164− ` It read: ${lead.text.trim()}`,
182+ ...(lead.text.trim() !== w.text.trim() ? [` It read: ${lead.text.trim()}`] : []),
165183 ]),
166184 ...(origin ? gitLines(origin) : []),
167185 ...issueLinks(`${w.text}\n${origin?.message ?? ""}`, origin?.issues).map((l) => ` ${l}`),
168186 ...(skipped.length > 0
169187 ? [
170− `Skipped ${skipped.length} formatting change${skipped.length === 1 ? "" : "s"} since: ${skipped.map((s) => stepName(s)).join("; ")}. abso why --all shows ${skipped.length === 1 ? "it" : "them"}.`,
188+ `Skipped ${skippedKinds(skipped)} since: ${skipped.map((s) => `${stepName(s)}${s.movedFrom ? `, moved from ${s.movedFrom}` : ""}`).join("; ")}. abso why --all shows ${skipped.length === 1 ? "it" : "them"}.`,
171189 ]
172190 : []),
173191 ...(earlier.length > 0
@@ -177,8 +195,8 @@
177195 const newer = i === 0 ? lead! : earlier[i - 1]!;
178196 const o = s.publish ? gitOrigin(s.publish, w.gitIssues) : undefined;
179197 return [
180− ` ${stepName(s, true)}${s.formatting ? " (formatting only)" : ""}${o ? `, from git commit ${o.commit.slice(0, 12)}${o.author ? ` by ${o.author}` : ""}` : ""}`,
181− ...(s.text !== newer.text ? [` It read: ${s.text.trim()}`] : []),
198+ ` ${stepName(s, true)}${s.movedFrom ? ` (moved from ${s.movedFrom})` : s.formatting ? " (formatting only)" : ""}${o ? `, from git commit ${o.commit.slice(0, 12)}${o.author ? ` by ${o.author}` : ""}` : ""}`,
199+ ...(s.text.trim() !== newer.text.trim() ? [` It read: ${s.text.trim()}`] : []),
182200 ];
183201 }),
184202 ]
@@ -209,7 +227,7 @@
209227 ]
210228 : []),
211229 ];
212− return { text: lines.join("\n"), json: { ...w } };
230+ return lines.join("\n");
213231}
214232
215233function ranges(rs: readonly [number, number][]): string {
--- a/worker/project/blame.ts
+++ b/worker/project/blame.ts
@@ -9,7 +9,7 @@
99import { pathKey } from "./pathkey.ts";
1010import type { Project } from "./project.ts";
1111import { projectFileIn } from "./project-helpers.ts";
12−import { trail } from "./trail.ts";
12+import { type TrailStep, trail } from "./trail.ts";
1313
1414export interface BlameLine {
1515 n: number;
@@ -257,43 +257,66 @@
257257
258258/**
259259 * The line's changes, newest first (abso-core issue 20), its own change
260− * first: each change that rewrote it, with the line's text after it, and
261− * whether it only changed formatting. Several changes in one push count as
260+ * first: each change that rewrote it, with the line's text after it,
261+ * whether it only changed formatting, and the file it moved from, if it did. Several changes in one push count as
262262 * one, formatting only if all of them were.
263263 */
264264function lineTrail(project: Project, view: ReadView, file: string, line: LineId) {
265265 const graph = project.graph;
266− const { steps, more } = trail(graph, view, file, line);
267− const out: {
268− atom: AtomId;
269− author: string;
270− message: string;
271− time: number;
272− text: string;
273− formatting: boolean;
274− publish: Publish | null;
275− }[] = [];
266+ // Changes pushed together: the same version of the same space, or both still in one agent's draft.
267+ const samePush = (a: Atom, b: Atom) => samePublish(publishOf(project, a.id), publishOf(project, b.id));
268+ const { steps, more } = trail(graph, view, file, line, undefined, samePush);
269+ const groups: { steps: TrailStep[]; publish: Publish | null }[] = [];
276270 for (const s of steps) {
277− const a = graph.atom(s.atom);
278− const publish = publishOf(project, a.id);
279− const prev = out[out.length - 1];
280− if (prev && samePublish(prev.publish, publish) && (publish !== null || prev.author === a.author)) {
281− prev.formatting &&= s.formatting;
282− continue;
283− }
284− out.push({
271+ const publish = publishOf(project, s.atom);
272+ const prev = groups[groups.length - 1];
273+ if (
274+ prev &&
275+ samePublish(prev.publish, publish) &&
276+ (publish !== null || graph.atom(prev.steps[0]!.atom).author === graph.atom(s.atom).author)
277+ )
278+ prev.steps.push(s);
279+ else groups.push({ steps: [s], publish });
280+ }
281+ const out = groups.map(({ steps: [newest, ...rest], publish }) => {
282+ const a = graph.atom(newest!.atom);
283+ const oldest = rest[rest.length - 1] ?? newest!;
284+ const moved = [newest!, ...rest].some((s) => s.maybeMoved)
285+ ? movedFrom(project, newest!.line, oldest.replaced, publish)
286+ : undefined;
287+ return {
285288 atom: a.id,
286289 author: a.author,
287290 message: a.message,
288291 time: a.time,
289− text: s.text,
290− formatting: s.formatting,
292+ text: newest!.text,
293+ formatting: [newest!, ...rest].every((s) => s.formatting),
294+ ...(moved !== undefined ? { movedFrom: moved } : {}),
291295 publish,
292− });
293− }
296+ };
297+ });
294298 return { trail: out, ...(more ? { trailMore: true as const } : {}) };
295299}
296300
301+/**
302+ * The file a push moved a line from: where the line it replaced was just
303+ * before the push, when that's not where the push left the line.
304+ */
305+function movedFrom(project: Project, line: LineId, replaced: LineId | undefined, publish: Publish | null) {
306+ if (replaced === undefined || publish === null || publish.version < 1) return undefined;
307+ const where = publish.draft === null ? {} : { draft: publish.draft };
308+ const fileIn = (version: number, id: LineId) => {
309+ try {
310+ return project.viewAt(version, where).fileOf(id);
311+ } catch {
312+ return undefined;
313+ }
314+ };
315+ const before = fileIn(publish.version - 1, replaced);
316+ const after = fileIn(publish.version, line);
317+ return before !== undefined && after !== undefined && before !== after ? before : undefined;
318+}
297320/** Where "#N" in history imported from git points (abso.json's "gitIssues"), when shared's project file says. */
298321function gitIssuesOf(view: ReadView): string | undefined {
299322 const file = projectFileIn(view);
--- a/worker/project/test/trail.test.ts
+++ b/worker/project/test/trail.test.ts
@@ -96,3 +96,41 @@
9696 assert.equal(t.more, true);
9797 assert.equal(trail(p.graph, view, "a.ts", line).steps.length, 5);
9898});
100+test("code moved to another file in one push is followed back into the file it came from", () => {
101+ const p = setup({
102+ "big.ts": "export const keep = 1;\nexport function tide(level: number) {\n\treturn level * 2;\n}\n",
103+ "small.ts": "export const other = 1;\n",
104+ });
105+ push(p, "ann", "big.ts", "level * 2", "level * 3", "Tides run higher", 10); // v1
106+ // Moved as it is: the engine keeps the lines themselves, so their history comes along.
107+ p.sync("bob");
108+ const tide = "export function tide(level: number) {\n\treturn level * 3;\n}\n";
109+ p.edit("bob", { file: "big.ts", old: tide, new: "" }, 20);
110+ p.edit("bob", { file: "small.ts", old: "export const other = 1;\n", new: `export const other = 1;\n${tide}` }, 21);
111+ p.publish("bob", "Move tide to small.ts", 22); // v2
112+ assert.deepEqual(
113+ why(p, "small.ts", 3).trail.map((s) => [s.publish?.version, s.text]),
114+ [
115+ [1, "\treturn level * 3;"],
116+ [0, "\treturn level * 2;"],
117+ ],
118+ );
119+ // Moved and reindented in one push: new lines, followed back by their code.
120+ p.sync("ann");
121+ const moved = tide.replaceAll("\t", " ");
122+ p.edit("ann", { file: "small.ts", old: tide, new: "" }, 30);
123+ p.edit("ann", { file: "big.ts", old: "export const keep = 1;\n", new: `export const keep = 1;\n${moved}` }, 31);
124+ p.publish("ann", "Move tide back, two-space indent", 32); // v3
125+ const w = why(p, "big.ts", 3);
126+ assert.deepEqual(
127+ w.trail.map((s) => [s.publish?.version, s.formatting, s.movedFrom, s.text]),
128+ [
129+ [3, true, "small.ts", " return level * 3;"],
130+ [1, false, undefined, "\treturn level * 3;"],
131+ [0, false, undefined, "\treturn level * 2;"],
132+ ],
133+ );
134+ // A short line (a brace) is too common to follow.
135+ assert.equal(why(p, "big.ts", 4).trail.length, 1);
136+});
--- a/worker/project/trail.ts
+++ b/worker/project/trail.ts
@@ -8,7 +8,9 @@
88// change that only touched formatting (whitespace, line wrapping, quotes,
99// trailing commas and semicolons, parentheses around one arrow parameter)
1010// is marked, so why can skip past it to the change that said what the line
11−// does.
11+// does. So is a move: code a push took out of one place and wrote, the same,
12+// in another (a file split in two), which the trail follows into the file it
13+// came from.
1214
1315import type { Atom, AtomId, Graph, LineId, ReadView, SeqLine } from "../../src/engine/index.ts";
1416
@@ -23,6 +25,12 @@
2325 readonly formatting: boolean;
2426 /** The line it replaced, when it replaced one. */
2527 readonly replaced?: LineId;
28+ /**
29+ * The change may have moved the line: the line it replaced was found
30+ * elsewhere, or was written in another file. Which file it was in then
31+ * takes the versions before and after (blame.ts).
32+ */
33+ readonly maybeMoved?: true;
2634}
2735
2836/** At most this many steps back. */
@@ -174,7 +182,10 @@
174182/**
175183 * The line's changes, newest first: the change that wrote it, then the one
176184 * 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.
185+ * new. A line written new where the same push deleted the same code
186+ * elsewhere (code moved to another file, or within one) follows that line
187+ * on; `samePush` says which changes were pushed together. At most `max`
188+ * steps; `more` says the trail went on.
178189 */
179190export function trail(
180191 graph: Graph,
@@ -182,36 +193,112 @@
182193 file: string,
183194 line: LineId,
184195 max = TRAIL_MAX,
196+ samePush: (a: Atom, b: Atom) => boolean = (a, b) => a.id === b.id,
185197): { 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);
198+ const files = new Map<string, { seq: readonly SeqLine[]; index: Map<LineId, number> }>();
199+ const sequenceOf = (path: string) => {
200+ let f = files.get(path);
201+ if (!f) {
202+ const seq = view.sequence(path);
203+ f = { seq, index: new Map(seq.map((s, i) => [s.id, i])) };
204+ files.set(path, f);
205+ }
206+ return f;
207+ };
189208 const steps: TrailStep[] = [];
190209 let current: LineId | undefined = line;
210+ let hint = file;
191211 while (current !== undefined && steps.length < max) {
192− const at = index.get(current);
193− if (at === undefined) break;
212+ // The file to read its hunk in: where the view has it, or where it was written, whichever pairs it.
213+ const id: LineId = current;
214+ let found: { seq: readonly SeqLine[]; at: number; hunk: { before: number[]; after: number[] } } | undefined;
215+ let pair: ReturnType<typeof pairIn>;
216+ for (const path of new Set([hint, fileIn(view, id), graph.lineFile(id)])) {
217+ if (path === undefined) continue;
218+ const { seq, index } = sequenceOf(path);
219+ const at = index.get(id);
220+ if (at === undefined) continue;
221+ const hunk = hunkOf(graph, seq, at, graph.atom(seq[at]!.atom));
222+ const p = pairIn(
223+ hunk.before.map((i) => seq[i]!.text),
224+ hunk.after.map((i) => seq[i]!.text),
225+ hunk.after.indexOf(at),
226+ );
227+ if (!found || p) [found, pair] = [{ seq, at, hunk }, p];
228+ if (p) break;
229+ }
230+ if (!found) break;
231+ const { seq, at, hunk } = found;
194232 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;
233+ let replaced = pair ? seq[hunk.before[pair.index]!]!.id : undefined;
234+ let formatting = pair?.formatting ?? false;
235+ let searched = false;
236+ if (replaced === undefined) {
237+ const moved = movedFrom(graph, view, atom, seq[at]!.text, new Set(hunk.before.map((i) => seq[i]!.id)), samePush);
238+ if (moved) [replaced, formatting, searched] = [moved, true, true];
239+ }
240+ const maybeMoved = searched || (replaced !== undefined && graph.lineFile(replaced) !== graph.lineFile(id));
202241 steps.push({
203242 atom: atom.id,
204− line: current,
243+ line: id,
205244 text: seq[at]!.text,
206− formatting: pair?.formatting ?? false,
245+ formatting,
207246 ...(replaced !== undefined ? { replaced } : {}),
247+ ...(maybeMoved ? { maybeMoved: true as const } : {}),
208248 });
249+ hint = replaced === undefined ? hint : (fileIn(view, replaced) ?? hint);
209250 current = replaced;
210251 }
211252 return { steps, more: current !== undefined };
212253}
213254
255+/** The file a line is in, in a view, or undefined when the view can't place it. */
256+function fileIn(view: ReadView, line: LineId): string | undefined {
257+ try {
258+ return view.fileOf(line);
259+ } catch {
260+ return undefined;
261+ }
262+}
264+/** How far either side of a change to look for the others pushed with it. */
265+const NEAR = 5000;
214267/**
268+ * Where a line written new came from, when it was moved: a line with the same
269+ * code (exactly, else formatted differently) that a change pushed with this
270+ * one deleted, if exactly one such line has the best match. Short lines
271+ * (a brace, a blank) match too much to follow.
272+ */
273+function movedFrom(
274+ graph: Graph,
275+ view: ReadView,
276+ atom: Atom,
277+ text: string,
278+ skip: ReadonlySet<LineId>,
279+ samePush: (a: Atom, b: Atom) => boolean,
280+): LineId | undefined {
281+ const want = normalize([text]);
282+ if (want.length < 12) return undefined;
283+ const all = graph.atoms();
284+ const exact: LineId[] = [];
285+ const same: LineId[] = [];
286+ for (let s = Math.max(0, atom.seq - NEAR); s <= Math.min(all.length - 1, atom.seq + NEAR); s++) {
287+ const other = all[s]!;
288+ if (other.author !== atom.author || !view.set.has(other.id) || !samePush(other, atom)) continue;
289+ for (const d of other.deletes) {
290+ if (skip.has(d)) continue;
291+ const t = graph.lineText(d);
292+ if (t === undefined) continue;
293+ if (t === text) exact.push(d);
294+ else if (normalize([t]) === want) same.push(d);
295+ }
296+ }
297+ const found = exact.length > 0 ? exact : same;
298+ return found.length === 1 ? found[0] : undefined;
299+}
301+/**
215302 * The hunk a change made around one of its lines, as indexes into the
216303 * sequence in file order: the lines it deleted there and the lines it wrote.
217304 * Lines nobody could see when it was made (written later, or already