{"version":194,"agent":"whytrail","message":"abso why follows code moved between files, and why's text for scripts\n\nA 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.\n\nwhyText (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.","time":1792020237034,"timeIso":"2026-10-14T23:23:57.034Z","changes":8,"change":3253,"files":["client/src/history-answers.ts","client/src/history.ts","worker/project/blame.ts","worker/project/test/trail.test.ts","worker/project/trail.ts"],"originalMessage":"abso why follows code moved between files, and why's text for scripts\n\nA 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.\n\nwhyText (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.","draft":null,"diff":[{"path":"client/src/history-answers.ts","added":3,"removed":0,"diff":"--- a/client/src/history-answers.ts\n+++ b/client/src/history-answers.ts\n@@ -166,7 +166,10 @@\n \treadonly message: string;\n \treadonly time: number;\n \treadonly text: string;\n+\t/** It only changed formatting, or only moved the line. */\n \treadonly formatting: boolean;\n+\t/** The file it moved the line from, when another. */\n+\treadonly movedFrom?: string;\n \treadonly publish: Publish | null;\n }\n export interface WhyAnswer {"},{"path":"client/src/history.ts","added":27,"removed":9,"diff":"--- a/client/src/history.ts\n+++ b/client/src/history.ts\n@@ -9,7 +9,7 @@\n import { findRoot, readConfig } from \"./config.ts\";\n import { relativeTo } from \"./folder.ts\";\n import { type GitOrigin, gitOrigin, issueLinks } from \"./git-origin.ts\";\n-import { byWhom, type TrailStep } from \"./history-answers.ts\";\n+import { byWhom, type TrailStep, type WhyAnswer } from \"./history-answers.ts\";\n \n function open(env: Env): { root: string; api: Api; agent: string } {\n \tconst root = findRoot(env.cwd);\n@@ -61,9 +61,20 @@\n \treturn [`  From git: commit ${o.commit}${o.author ? ` by ${o.author}` : \"\"}${o.time ? `, ${o.time}` : \"\"}.`];\n }\n \n+/** \"1 formatting change and 2 moves\": what why skipped. */\n+function skippedKinds(steps: readonly TrailStep[]): string {\n+\tconst moves = steps.filter((s) => s.movedFrom !== undefined).length;\n+\tconst formatting = steps.length - moves;\n+\treturn [\n+\t\t...(formatting > 0 ? [`${formatting} formatting change${formatting === 1 ? \"\" : \"s\"}`] : []),\n+\t\t...(moves > 0 ? [`${moves} move${moves === 1 ? \"\" : \"s\"}`] : []),\n+\t].join(\" and \");\n+}\n+\n /** A trail step in a few words: its version (or that it isn't pushed), who, and the first line of why. */\n function stepName(s: TrailStep, long = false): string {\n-\tconst subject = (s.publish ? (gitOrigin(s.publish)?.message ?? s.publish.message) : s.message).split(\"\\n\")[0]!;\n+\tconst first = (s.publish ? (gitOrigin(s.publish)?.message ?? s.publish.message) : s.message).split(\"\\n\")[0]!;\n+\tconst subject = first.length > 100 ? `${first.slice(0, 97).trimEnd()}...` : first;\n \tconst where = s.publish === null ? \"not pushed\" : `version ${s.publish.version}`;\n \tconst who = s.publish ? byWhom(s.publish) : s.author;\n \treturn long\n@@ -147,8 +158,15 @@\n \tconst w = await api.why(file, lineNumber(raw, \"the line\")!, view, at);\n \tif (at !== undefined && w.version !== at)\n \t\tthrow new UsageError(`this server doesn't read why at a version yet; abso blame ${w.file} --version ${at} does`);\n-\t// The line's changes (abso-core issue 20): the newest one that changed what it does leads, past formatting-only ones.\n-\tconst all = bool(args, \"all\");\n+\treturn { text: whyText(w, { at, all: bool(args, \"all\") }), json: { ...w } };\n+}\n+\n+/**\n+ * What abso why prints for an answer. The line's changes (abso-core issue\n+ * 20): the newest one that changed what it does leads, past formatting-only\n+ * ones, unless `all`.\n+ */\n+export function whyText(w: WhyAnswer, { at, all = false }: { at?: number; all?: boolean } = {}): string {\n \tconst trail = w.trail ?? [];\n \tconst lead = (all ? trail[0] : trail.find((s) => !s.formatting)) ?? trail[0];\n \tconst leadAt = lead === undefined ? 0 : trail.indexOf(lead);\n@@ -161,13 +179,13 @@\n \t\t\t? [`Written by ${w.change.author} (${when(w.change.time)}), pushed in ${pushed(w.publish, w.gitIssues)}.`]\n \t\t\t: [\n \t\t\t\t\t`Written by ${lead.author} (${when(lead.time)}), pushed in ${pushed(lead.publish, w.gitIssues)}.`,\n-\t\t\t\t\t`  It read: ${lead.text.trim()}`,\n+\t\t\t\t\t...(lead.text.trim() !== w.text.trim() ? [`  It read: ${lead.text.trim()}`] : []),\n \t\t\t\t]),\n \t\t...(origin ? gitLines(origin) : []),\n \t\t...issueLinks(`${w.text}\\n${origin?.message ?? \"\"}`, origin?.issues).map((l) => `  ${l}`),\n \t\t...(skipped.length > 0\n \t\t\t? [\n-\t\t\t\t\t`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\"}.`,\n+\t\t\t\t\t`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\"}.`,\n \t\t\t\t]\n \t\t\t: []),\n \t\t...(earlier.length > 0\n@@ -177,8 +195,8 @@\n \t\t\t\t\t\tconst newer = i === 0 ? lead! : earlier[i - 1]!;\n \t\t\t\t\t\tconst o = s.publish ? gitOrigin(s.publish, w.gitIssues) : undefined;\n \t\t\t\t\t\treturn [\n-\t\t\t\t\t\t\t`  ${stepName(s, true)}${s.formatting ? \" (formatting only)\" : \"\"}${o ? `, from git commit ${o.commit.slice(0, 12)}${o.author ? ` by ${o.author}` : \"\"}` : \"\"}`,\n-\t\t\t\t\t\t\t...(s.text !== newer.text ? [`    It read: ${s.text.trim()}`] : []),\n+\t\t\t\t\t\t\t`  ${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}` : \"\"}` : \"\"}`,\n+\t\t\t\t\t\t\t...(s.text.trim() !== newer.text.trim() ? [`    It read: ${s.text.trim()}`] : []),\n \t\t\t\t\t\t];\n \t\t\t\t\t}),\n \t\t\t\t]\n@@ -209,7 +227,7 @@\n \t\t\t\t]\n \t\t\t: []),\n \t];\n-\treturn { text: lines.join(\"\\n\"), json: { ...w } };\n+\treturn lines.join(\"\\n\");\n }\n \n function ranges(rs: readonly [number, number][]): string {"},{"path":"worker/project/blame.ts","added":48,"removed":25,"diff":"--- a/worker/project/blame.ts\n+++ b/worker/project/blame.ts\n@@ -9,7 +9,7 @@\n import { pathKey } from \"./pathkey.ts\";\n import type { Project } from \"./project.ts\";\n import { projectFileIn } from \"./project-helpers.ts\";\n-import { trail } from \"./trail.ts\";\n+import { type TrailStep, trail } from \"./trail.ts\";\n \n export interface BlameLine {\n \tn: number;\n@@ -257,43 +257,66 @@\n \n /**\n  * The line's changes, newest first (abso-core issue 20), its own change\n- * first: each change that rewrote it, with the line's text after it, and\n- * whether it only changed formatting. Several changes in one push count as\n+ * first: each change that rewrote it, with the line's text after it,\n+ * whether it only changed formatting, and the file it moved from, if it did. Several changes in one push count as\n  * one, formatting only if all of them were.\n  */\n function lineTrail(project: Project, view: ReadView, file: string, line: LineId) {\n \tconst graph = project.graph;\n-\tconst { steps, more } = trail(graph, view, file, line);\n-\tconst out: {\n-\t\tatom: AtomId;\n-\t\tauthor: string;\n-\t\tmessage: string;\n-\t\ttime: number;\n-\t\ttext: string;\n-\t\tformatting: boolean;\n-\t\tpublish: Publish | null;\n-\t}[] = [];\n+\t// Changes pushed together: the same version of the same space, or both still in one agent's draft.\n+\tconst samePush = (a: Atom, b: Atom) => samePublish(publishOf(project, a.id), publishOf(project, b.id));\n+\tconst { steps, more } = trail(graph, view, file, line, undefined, samePush);\n+\tconst groups: { steps: TrailStep[]; publish: Publish | null }[] = [];\n \tfor (const s of steps) {\n-\t\tconst a = graph.atom(s.atom);\n-\t\tconst publish = publishOf(project, a.id);\n-\t\tconst prev = out[out.length - 1];\n-\t\tif (prev && samePublish(prev.publish, publish) && (publish !== null || prev.author === a.author)) {\n-\t\t\tprev.formatting &&= s.formatting;\n-\t\t\tcontinue;\n-\t\t}\n-\t\tout.push({\n+\t\tconst publish = publishOf(project, s.atom);\n+\t\tconst prev = groups[groups.length - 1];\n+\t\tif (\n+\t\t\tprev &&\n+\t\t\tsamePublish(prev.publish, publish) &&\n+\t\t\t(publish !== null || graph.atom(prev.steps[0]!.atom).author === graph.atom(s.atom).author)\n+\t\t)\n+\t\t\tprev.steps.push(s);\n+\t\telse groups.push({ steps: [s], publish });\n+\t}\n+\tconst out = groups.map(({ steps: [newest, ...rest], publish }) => {\n+\t\tconst a = graph.atom(newest!.atom);\n+\t\tconst oldest = rest[rest.length - 1] ?? newest!;\n+\t\tconst moved = [newest!, ...rest].some((s) => s.maybeMoved)\n+\t\t\t? movedFrom(project, newest!.line, oldest.replaced, publish)\n+\t\t\t: undefined;\n+\t\treturn {\n \t\t\tatom: a.id,\n \t\t\tauthor: a.author,\n \t\t\tmessage: a.message,\n \t\t\ttime: a.time,\n-\t\t\ttext: s.text,\n-\t\t\tformatting: s.formatting,\n+\t\t\ttext: newest!.text,\n+\t\t\tformatting: [newest!, ...rest].every((s) => s.formatting),\n+\t\t\t...(moved !== undefined ? { movedFrom: moved } : {}),\n \t\t\tpublish,\n-\t\t});\n-\t}\n+\t\t};\n+\t});\n \treturn { trail: out, ...(more ? { trailMore: true as const } : {}) };\n }\n \n+/**\n+ * The file a push moved a line from: where the line it replaced was just\n+ * before the push, when that's not where the push left the line.\n+ */\n+function movedFrom(project: Project, line: LineId, replaced: LineId | undefined, publish: Publish | null) {\n+\tif (replaced === undefined || publish === null || publish.version < 1) return undefined;\n+\tconst where = publish.draft === null ? {} : { draft: publish.draft };\n+\tconst fileIn = (version: number, id: LineId) => {\n+\t\ttry {\n+\t\t\treturn project.viewAt(version, where).fileOf(id);\n+\t\t} catch {\n+\t\t\treturn undefined;\n+\t\t}\n+\t};\n+\tconst before = fileIn(publish.version - 1, replaced);\n+\tconst after = fileIn(publish.version, line);\n+\treturn before !== undefined && after !== undefined && before !== after ? before : undefined;\n+}\n+\n /** Where \"#N\" in history imported from git points (abso.json's \"gitIssues\"), when shared's project file says. */\n function gitIssuesOf(view: ReadView): string | undefined {\n \tconst file = projectFileIn(view);"},{"path":"worker/project/test/trail.test.ts","added":38,"removed":0,"diff":"--- a/worker/project/test/trail.test.ts\n+++ b/worker/project/test/trail.test.ts\n@@ -96,3 +96,41 @@\n \tassert.equal(t.more, true);\n \tassert.equal(trail(p.graph, view, \"a.ts\", line).steps.length, 5);\n });\n+\n+test(\"code moved to another file in one push is followed back into the file it came from\", () => {\n+\tconst p = setup({\n+\t\t\"big.ts\": \"export const keep = 1;\\nexport function tide(level: number) {\\n\\treturn level * 2;\\n}\\n\",\n+\t\t\"small.ts\": \"export const other = 1;\\n\",\n+\t});\n+\tpush(p, \"ann\", \"big.ts\", \"level * 2\", \"level * 3\", \"Tides run higher\", 10); // v1\n+\t// Moved as it is: the engine keeps the lines themselves, so their history comes along.\n+\tp.sync(\"bob\");\n+\tconst tide = \"export function tide(level: number) {\\n\\treturn level * 3;\\n}\\n\";\n+\tp.edit(\"bob\", { file: \"big.ts\", old: tide, new: \"\" }, 20);\n+\tp.edit(\"bob\", { file: \"small.ts\", old: \"export const other = 1;\\n\", new: `export const other = 1;\\n${tide}` }, 21);\n+\tp.publish(\"bob\", \"Move tide to small.ts\", 22); // v2\n+\tassert.deepEqual(\n+\t\twhy(p, \"small.ts\", 3).trail.map((s) => [s.publish?.version, s.text]),\n+\t\t[\n+\t\t\t[1, \"\\treturn level * 3;\"],\n+\t\t\t[0, \"\\treturn level * 2;\"],\n+\t\t],\n+\t);\n+\t// Moved and reindented in one push: new lines, followed back by their code.\n+\tp.sync(\"ann\");\n+\tconst moved = tide.replaceAll(\"\\t\", \"  \");\n+\tp.edit(\"ann\", { file: \"small.ts\", old: tide, new: \"\" }, 30);\n+\tp.edit(\"ann\", { file: \"big.ts\", old: \"export const keep = 1;\\n\", new: `export const keep = 1;\\n${moved}` }, 31);\n+\tp.publish(\"ann\", \"Move tide back, two-space indent\", 32); // v3\n+\tconst w = why(p, \"big.ts\", 3);\n+\tassert.deepEqual(\n+\t\tw.trail.map((s) => [s.publish?.version, s.formatting, s.movedFrom, s.text]),\n+\t\t[\n+\t\t\t[3, true, \"small.ts\", \"  return level * 3;\"],\n+\t\t\t[1, false, undefined, \"\\treturn level * 3;\"],\n+\t\t\t[0, false, undefined, \"\\treturn level * 2;\"],\n+\t\t],\n+\t);\n+\t// A short line (a brace) is too common to follow.\n+\tassert.equal(why(p, \"big.ts\", 4).trail.length, 1);\n+});"},{"path":"worker/project/trail.ts","added":103,"removed":16,"diff":"--- a/worker/project/trail.ts\n+++ b/worker/project/trail.ts\n@@ -8,7 +8,9 @@\n // change that only touched formatting (whitespace, line wrapping, quotes,\n // trailing commas and semicolons, parentheses around one arrow parameter)\n // is marked, so why can skip past it to the change that said what the line\n-// does.\n+// does. So is a move: code a push took out of one place and wrote, the same,\n+// in another (a file split in two), which the trail follows into the file it\n+// came from.\n \n import type { Atom, AtomId, Graph, LineId, ReadView, SeqLine } from \"../../src/engine/index.ts\";\n \n@@ -23,6 +25,12 @@\n \treadonly formatting: boolean;\n \t/** The line it replaced, when it replaced one. */\n \treadonly replaced?: LineId;\n+\t/**\n+\t * The change may have moved the line: the line it replaced was found\n+\t * elsewhere, or was written in another file. Which file it was in then\n+\t * takes the versions before and after (blame.ts).\n+\t */\n+\treadonly maybeMoved?: true;\n }\n \n /** At most this many steps back. */\n@@ -174,7 +182,10 @@\n /**\n  * The line's changes, newest first: the change that wrote it, then the one\n  * that wrote the line it replaced, and so on back to a change that wrote it\n- * new. At most `max` steps; `more` says the trail went on.\n+ * new. A line written new where the same push deleted the same code\n+ * elsewhere (code moved to another file, or within one) follows that line\n+ * on; `samePush` says which changes were pushed together. At most `max`\n+ * steps; `more` says the trail went on.\n  */\n export function trail(\n \tgraph: Graph,\n@@ -182,36 +193,112 @@\n \tfile: string,\n \tline: LineId,\n \tmax = TRAIL_MAX,\n+\tsamePush: (a: Atom, b: Atom) => boolean = (a, b) => a.id === b.id,\n ): { steps: TrailStep[]; more: boolean } {\n-\tconst seq = view.sequence(file);\n-\tconst index = new Map<LineId, number>();\n-\tfor (const [i, s] of seq.entries()) index.set(s.id, i);\n+\tconst files = new Map<string, { seq: readonly SeqLine[]; index: Map<LineId, number> }>();\n+\tconst sequenceOf = (path: string) => {\n+\t\tlet f = files.get(path);\n+\t\tif (!f) {\n+\t\t\tconst seq = view.sequence(path);\n+\t\t\tf = { seq, index: new Map(seq.map((s, i) => [s.id, i])) };\n+\t\t\tfiles.set(path, f);\n+\t\t}\n+\t\treturn f;\n+\t};\n \tconst steps: TrailStep[] = [];\n \tlet current: LineId | undefined = line;\n+\tlet hint = file;\n \twhile (current !== undefined && steps.length < max) {\n-\t\tconst at = index.get(current);\n-\t\tif (at === undefined) break;\n+\t\t// The file to read its hunk in: where the view has it, or where it was written, whichever pairs it.\n+\t\tconst id: LineId = current;\n+\t\tlet found: { seq: readonly SeqLine[]; at: number; hunk: { before: number[]; after: number[] } } | undefined;\n+\t\tlet pair: ReturnType<typeof pairIn>;\n+\t\tfor (const path of new Set([hint, fileIn(view, id), graph.lineFile(id)])) {\n+\t\t\tif (path === undefined) continue;\n+\t\t\tconst { seq, index } = sequenceOf(path);\n+\t\t\tconst at = index.get(id);\n+\t\t\tif (at === undefined) continue;\n+\t\t\tconst hunk = hunkOf(graph, seq, at, graph.atom(seq[at]!.atom));\n+\t\t\tconst p = pairIn(\n+\t\t\t\thunk.before.map((i) => seq[i]!.text),\n+\t\t\t\thunk.after.map((i) => seq[i]!.text),\n+\t\t\t\thunk.after.indexOf(at),\n+\t\t\t);\n+\t\t\tif (!found || p) [found, pair] = [{ seq, at, hunk }, p];\n+\t\t\tif (p) break;\n+\t\t}\n+\t\tif (!found) break;\n+\t\tconst { seq, at, hunk } = found;\n \t\tconst atom = graph.atom(seq[at]!.atom);\n-\t\tconst hunk = hunkOf(graph, seq, at, atom);\n-\t\tconst pair = pairIn(\n-\t\t\thunk.before.map((i) => seq[i]!.text),\n-\t\t\thunk.after.map((i) => seq[i]!.text),\n-\t\t\thunk.after.indexOf(at),\n-\t\t);\n-\t\tconst replaced = pair ? seq[hunk.before[pair.index]!]!.id : undefined;\n+\t\tlet replaced = pair ? seq[hunk.before[pair.index]!]!.id : undefined;\n+\t\tlet formatting = pair?.formatting ?? false;\n+\t\tlet searched = false;\n+\t\tif (replaced === undefined) {\n+\t\t\tconst moved = movedFrom(graph, view, atom, seq[at]!.text, new Set(hunk.before.map((i) => seq[i]!.id)), samePush);\n+\t\t\tif (moved) [replaced, formatting, searched] = [moved, true, true];\n+\t\t}\n+\t\tconst maybeMoved = searched || (replaced !== undefined && graph.lineFile(replaced) !== graph.lineFile(id));\n \t\tsteps.push({\n \t\t\tatom: atom.id,\n-\t\t\tline: current,\n+\t\t\tline: id,\n \t\t\ttext: seq[at]!.text,\n-\t\t\tformatting: pair?.formatting ?? false,\n+\t\t\tformatting,\n \t\t\t...(replaced !== undefined ? { replaced } : {}),\n+\t\t\t...(maybeMoved ? { maybeMoved: true as const } : {}),\n \t\t});\n+\t\thint = replaced === undefined ? hint : (fileIn(view, replaced) ?? hint);\n \t\tcurrent = replaced;\n \t}\n \treturn { steps, more: current !== undefined };\n }\n \n+/** The file a line is in, in a view, or undefined when the view can't place it. */\n+function fileIn(view: ReadView, line: LineId): string | undefined {\n+\ttry {\n+\t\treturn view.fileOf(line);\n+\t} catch {\n+\t\treturn undefined;\n+\t}\n+}\n+\n+/** How far either side of a change to look for the others pushed with it. */\n+const NEAR = 5000;\n+\n /**\n+ * Where a line written new came from, when it was moved: a line with the same\n+ * code (exactly, else formatted differently) that a change pushed with this\n+ * one deleted, if exactly one such line has the best match. Short lines\n+ * (a brace, a blank) match too much to follow.\n+ */\n+function movedFrom(\n+\tgraph: Graph,\n+\tview: ReadView,\n+\tatom: Atom,\n+\ttext: string,\n+\tskip: ReadonlySet<LineId>,\n+\tsamePush: (a: Atom, b: Atom) => boolean,\n+): LineId | undefined {\n+\tconst want = normalize([text]);\n+\tif (want.length < 12) return undefined;\n+\tconst all = graph.atoms();\n+\tconst exact: LineId[] = [];\n+\tconst same: LineId[] = [];\n+\tfor (let s = Math.max(0, atom.seq - NEAR); s <= Math.min(all.length - 1, atom.seq + NEAR); s++) {\n+\t\tconst other = all[s]!;\n+\t\tif (other.author !== atom.author || !view.set.has(other.id) || !samePush(other, atom)) continue;\n+\t\tfor (const d of other.deletes) {\n+\t\t\tif (skip.has(d)) continue;\n+\t\t\tconst t = graph.lineText(d);\n+\t\t\tif (t === undefined) continue;\n+\t\t\tif (t === text) exact.push(d);\n+\t\t\telse if (normalize([t]) === want) same.push(d);\n+\t\t}\n+\t}\n+\tconst found = exact.length > 0 ? exact : same;\n+\treturn found.length === 1 ? found[0] : undefined;\n+}\n+\n+/**\n  * The hunk a change made around one of its lines, as indexes into the\n  * sequence in file order: the lines it deleted there and the lines it wrote.\n  * Lines nobody could see when it was made (written later, or already"}],"ci":null}