{"version":193,"agent":"whytrail","message":"abso why follows a line back past formatting to its reason, and names git commits\n\nwhy now lists a line's earlier changes, newest first, with what the line read before each. A change that rewrote a line left the line it replaced in the file's history; why pairs the two (same code formatted differently, the same code rewrapped, or the most similar line) and follows the pairs back. A change that only touched formatting (whitespace, line wrapping, quotes, trailing commas, line-end semicolons, parentheses around a lone arrow parameter) is skipped by default, and why names it: 'Skipped 1 formatting change since: version 366'. --all shows those too. --at <version> reads the line as it was at that version.\n\nA push imported from git (client/scripts/import-git.ts, or a plain git push) names its commit and author in why and blame. import-git.ts now also records where the repository's issues are, from origin on GitHub or GitLab or --issues, and why says which issue #N in an imported message or line means. A project imported before this can say it once in abso.json: \"gitIssues\": \"https://github.com/owner/repo/issues\".\n\nCloses issues 20 and 21's tool side.","time":1792019638939,"timeIso":"2026-10-14T23:13:58.939Z","changes":32,"change":3243,"files":["client/README.md","client/scripts/import-git.ts","client/src/api.ts","client/src/git-origin.ts","client/src/history-answers.ts","client/src/history.ts","client/src/main.ts","client/test/fake-server.ts","client/test/why-trail.test.ts","server/src/engine-project.ts","worker/project/blame.ts","worker/project/http.ts","worker/project/project-api.ts","worker/project/project-helpers.ts","worker/project/test/trail.test.ts","worker/project/trail.ts"],"draft":null,"diff":[{"path":"client/README.md","added":2,"removed":2,"diff":"--- a/client/README.md\n+++ b/client/README.md\n@@ -137,8 +137,8 @@\n | `abso announce [--to a,b] <text>` | Tells the project's other agents (or those named) something. Each gets \"Announcement from you (as claimed): ...\" once, as news on their next edit, or within 30 seconds where the mod runs. |\n | `abso news` | The news waiting for you (collisions, changed definitions, moved lines, announcements), each item once. |\n | `abso where` | Who is working where: each file with unpushed changes, whose they are, and which lines. |\n-| `abso blame <file> [--from n] [--to n] [--version n]` | Who wrote each line of the file as shared has it (or had it at a version), and the push that brought it in. `--draft <name>` reads a named draft; `--mine` reads your own view and marks your unpushed lines with `*`. |\n-| `abso why <file>:<line>` | Why one line of shared is the way it is: its change, its push, its reasons, and any collision it settled. Takes `--draft` and `--mine` as blame does. |\n+| `abso blame <file> [--from n] [--to n] [--version n]` | Who wrote each line of the file as shared has it (or had it at a version), and the push that brought it in. A push imported from git names its commit and author. `--draft <name>` reads a named draft; `--mine` reads your own view and marks your unpushed lines with `*`. |\n+| `abso why <file>:<line> [--at n] [--all]` | Why one line of shared is the way it is: the newest change to what it does, its push, its reasons, and any collision it settled, then the line's earlier changes, newest first, with what it read before each. A change that only touched formatting (whitespace, line wrapping, quotes, trailing commas, line-end semicolons) is skipped, and why names it; `--all` shows those too. A line imported from git names its commit and author, and `#39` in it names the old repository's issue when the import recorded where those are, or when abso.json says so with `\"gitIssues\": \"https://github.com/owner/repo/issues\"`. `--at <version>` reads the line as it was at that version, as blame's `--version` does. Takes `--draft` and `--mine` as blame does. |\n | `abso drop <version...> -m \"why\" [--with-dependents]` | Takes pushed versions back out of shared as a new version, like `git revert`; history keeps them. Refused (exit 2) when later versions build on them (`--with-dependents` drops those too) or when anyone's unpushed work builds on them. |\n | `abso pluck <draft> <version...> -m \"why\" [--with-dependencies]` | Takes some versions of a named draft into shared as one version, like `git cherry-pick`. The draft stays as it is. |\n | `abso amend -m \"new message\"` | Corrects the message of your newest push in this space. The log keeps the first message. |"},{"path":"client/scripts/import-git.ts","added":28,"removed":4,"diff":"--- a/client/scripts/import-git.ts\n+++ b/client/scripts/import-git.ts\n@@ -2,7 +2,15 @@\n // first used for #56, abso hosts abso). Each commit on the first-parent line\n // of --ref becomes a version of shared, oldest first: the first by `avcs\n // init`, each later one by a publish of that commit's tree, with the commit's\n-// message and a last line naming the commit, its author and its time.\n+// message and a last line naming the commit, its author and its time. When\n+// the repository's issues are on the web (origin on GitHub or GitLab, or\n+// --issues <url>), a line after it says where, so \"#39\" in the message, or\n+// in the lines it wrote, names that tracker's issue, not the project's:\n+//\n+//   git 4e1a7c2b9f03, Ada Lovelace, 2026-10-06T20:36:43-05:00\n+//   Issues: https://github.com/owner/repo/issues\n+//\n+// abso why and abso blame read these lines (client/src/git-origin.ts).\n // --last-only imports the last commit alone.\n //\n // Before anything is created, the last commit is checked: abso stores text\n@@ -14,7 +22,9 @@\n // What a first cut leaves for later (#58): every version's agent is the\n // importing agent and its time is the import's (the commit's author and time\n // are in the message); merges come in as their first parent's line; a run\n-// that stops partway goes on with --after <sha> in the same --dir.\n+// that stops partway goes on with --after <sha> in the same --dir. Version 0\n+// is made by init, which says \"Start the project\", so the first commit's own\n+// message and hash aren't on it: they're printed when the import starts.\n //\n // CI counts each imported version as seen as soon as it lands, so it never\n // checks or bisects the history; it checks what is published after the\n@@ -22,7 +32,7 @@\n //\n //   node client/scripts/import-git.ts --project <name> [--ref origin/main]\n //     [--server <url>] [--agent import] [--dir <empty folder>]\n-//     [--last-only] [--check-only] [--after <sha>]\n+//     [--last-only] [--check-only] [--after <sha>] [--issues <url>]\n \n import { spawnSync } from \"node:child_process\";\n import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync } from \"node:fs\";\n@@ -31,6 +41,7 @@\n import { parseArgs } from \"node:util\";\n import { open } from \"../src/commands.ts\";\n import { loadIgnore } from \"../src/folder.ts\";\n+import { issuesOf } from \"../src/git-origin.ts\";\n import { main } from \"../src/main.ts\";\n \n const { values } = parseArgs({\n@@ -43,6 +54,7 @@\n \t\t\"last-only\": { type: \"boolean\", default: false },\n \t\t\"check-only\": { type: \"boolean\", default: false },\n \t\tafter: { type: \"string\" },\n+\t\tissues: { type: \"string\" },\n \t},\n });\n const checkOnly = values[\"check-only\"] === true;\n@@ -94,12 +106,18 @@\n \tprocess.exit(0);\n }\n \n+// Where \"#N\" in the history points: --issues, else origin's issues on GitHub or GitLab.\n+if (values.issues !== undefined && !/^https?:\\/\\/\\S+$/.test(values.issues))\n+\tfail(`--issues is a web address, like https://github.com/owner/repo/issues (got ${JSON.stringify(values.issues)})`);\n+const tracker = values.issues ?? issuesOf(origin());\n+\n const env = { cwd: dir, env: process.env, write: () => {} };\n let skipped = 0;\n for (let i = start; i < line.length; i++) {\n \tconst sha = line[i]!;\n \textract(sha, dir);\n-\tconst message = `${git([\"log\", \"-1\", \"--format=%B\", sha]).trim()}\\n\\ngit ${sha.slice(0, 12)}, ${git([\"log\", \"-1\", \"--format=%an, %aI\", sha]).trim()}`;\n+\tconst message = `${git([\"log\", \"-1\", \"--format=%B\", sha]).trim()}\\n\\ngit ${sha.slice(0, 12)}, ${git([\"log\", \"-1\", \"--format=%an, %aI\", sha]).trim()}${tracker ? `\\nIssues: ${tracker}` : \"\"}`;\n+\tif (i === 0) console.log(`Version 0 is commit ${sha.slice(0, 12)}: ${message.split(\"\\n\")[0]}`);\n \tconst subject = message.split(\"\\n\")[0]!.slice(0, 72);\n \t// The first commit makes the project; --as-is adds no starting .absoignore or avcs.json git never had.\n \tconst argv =\n@@ -179,6 +197,12 @@\n \treturn r.stdout;\n }\n \n+/** origin's address, or \"\" when there's no origin. */\n+function origin(): string {\n+\tconst r = spawnSync(\"git\", [\"remote\", \"get-url\", \"origin\"], { encoding: \"utf8\" });\n+\treturn r.status === 0 ? r.stdout.trim() : \"\";\n+}\n+\n function fail(message: string): never {\n \tconsole.error(message);\n \tprocess.exit(1);"},{"path":"client/src/api.ts","added":2,"removed":2,"diff":"--- a/client/src/api.ts\n+++ b/client/src/api.ts\n@@ -925,8 +925,8 @@\n \t\treturn this.json(\"POST\", this.agentPath(\"restore\"), files === null ? { all: true } : { files });\n \t}\n \n-\twhy(file: string, line: number, view: View = {}): Promise<WhyAnswer> {\n-\t\treturn this.json(\"GET\", `/why?${query({ file, line, ...view })}`);\n+\twhy(file: string, line: number, view: View = {}, version?: number): Promise<WhyAnswer> {\n+\t\treturn this.json(\"GET\", `/why?${query({ file, line, version, ...view })}`);\n \t}\n \n \twhere(): Promise<WhereAnswer> {"},{"path":"client/src/git-origin.ts","added":90,"removed":0,"diff":"--- /dev/null\n+++ b/client/src/git-origin.ts\n@@ -0,0 +1,90 @@\n+// Where a push came from in git, when it was imported (abso-core issue 21).\n+// A version imported by client/scripts/import-git.ts ends its message with\n+// a line naming the commit, its author and its time, and, when the\n+// repository's issues are on the web, a line saying where:\n+//\n+//   git 4e1a7c2b9f03, Ada Lovelace, 2026-10-06T20:36:43-05:00\n+//   Issues: https://github.com/owner/repo/issues\n+//\n+// A commit pushed with plain git ends with \"Git-Commit: <sha>\" and\n+// \"Author: <name>\". A host may also name the commit beside the version\n+// (`commit`). Issue references like \"#39\" in an imported message, or in a\n+// line it wrote, mean that repository's issues, not the project's.\n+\n+export interface GitOrigin {\n+\treadonly commit: string;\n+\treadonly author?: string;\n+\treadonly time?: string;\n+\t/** The repository's issues, as a web address, when known. */\n+\treadonly issues?: string;\n+\t/** The message with these lines taken off. */\n+\treadonly message: string;\n+}\n+\n+const IMPORTED = /^git ([0-9a-f]{7,64}), (.+), (\\d{4}-\\d\\d-\\d\\dT[^,\\s]+)$/;\n+const ISSUES = /^Issues: (https?:\\/\\/\\S+)$/;\n+const PUSHED = /^Git-Commit: ([0-9a-f]{7,64})$/;\n+const PUSHED_BY = /^Author: (.+)$/;\n+\n+/**\n+ * A push's git origin, from its message's last lines or the commit its host\n+ * names, or undefined when it didn't come from git. `issues` is where \"#N\"\n+ * points when the message doesn't say (the project's \"gitIssues\").\n+ */\n+export function gitOrigin(\n+\tp: { readonly message: string; readonly commit?: string; readonly author?: string },\n+\tissues?: string,\n+): GitOrigin | undefined {\n+\tconst lines = p.message.trimEnd().split(\"\\n\");\n+\tlet end = lines.length;\n+\tlet found: Omit<GitOrigin, \"message\"> | undefined;\n+\tconst tracker = ISSUES.exec(lines[end - 1] ?? \"\");\n+\tif (tracker) end--;\n+\tconst imported = IMPORTED.exec(lines[end - 1] ?? \"\");\n+\tif (imported) {\n+\t\tend--;\n+\t\tfound = { commit: imported[1]!, author: imported[2]!, time: imported[3]! };\n+\t} else {\n+\t\tconst by = PUSHED_BY.exec(lines[end - 1] ?? \"\");\n+\t\tconst sha = PUSHED.exec(lines[end - (by ? 2 : 1)] ?? \"\");\n+\t\tif (sha) {\n+\t\t\tend -= by ? 2 : 1;\n+\t\t\tfound = { commit: sha[1]!, ...(by ? { author: by[1]! } : {}) };\n+\t\t}\n+\t}\n+\tif (!found && p.commit !== undefined) found = { commit: p.commit, ...(p.author ? { author: p.author } : {}) };\n+\tif (!found) return undefined;\n+\tconst where = tracker?.[1] ?? issues;\n+\treturn {\n+\t\t...found,\n+\t\t...(where ? { issues: where.replace(/\\/+$/, \"\") } : {}),\n+\t\tmessage: lines.slice(0, end).join(\"\\n\").trimEnd(),\n+\t};\n+}\n+\n+/** A git remote's issues on the web: GitHub's or GitLab's, by https or ssh; else undefined. */\n+export function issuesOf(remote: string): string | undefined {\n+\tconst plain = remote\n+\t\t.trim()\n+\t\t.replace(/\\/+$/, \"\")\n+\t\t.replace(/\\.git$/, \"\");\n+\tconst m = /^(?:https:\\/\\/|ssh:\\/\\/git@|git@)(github\\.com|gitlab\\.com)[:/]([\\w.-]+(?:\\/[\\w.-]+)+)$/.exec(plain);\n+\tif (!m) return undefined;\n+\treturn m[1] === \"gitlab.com\" ? `https://gitlab.com/${m[2]}/-/issues` : `https://github.com/${m[2]}/issues`;\n+}\n+\n+/** The issues \"#N\" names in text, each once, in order: not \"owner/repo#N\", which names its own. */\n+export function issueNumbers(text: string): number[] {\n+\tconst out: number[] = [];\n+\tfor (const m of text.matchAll(/(^|[^\\w/])#(\\d+)\\b/g)) {\n+\t\tconst n = Number(m[2]);\n+\t\tif (!out.includes(n)) out.push(n);\n+\t}\n+\treturn out;\n+}\n+\n+/** One line per \"#N\" in text, saying which tracker it's in. Empty without a tracker or a reference. */\n+export function issueLinks(text: string, issues: string | undefined): string[] {\n+\tif (!issues) return [];\n+\treturn issueNumbers(text).map((n) => `#${n} is ${issues}/${n}`);\n+}"},{"path":"client/src/history-answers.ts","added":20,"removed":0,"diff":"--- a/client/src/history-answers.ts\n+++ b/client/src/history-answers.ts\n@@ -129,6 +129,8 @@\n \treadonly agent: string;\n \t/** Who first wrote it, when the project's history was imported (abso issue 23). */\n \treadonly author?: string;\n+\t/** The git commit it came from, when its host knows it. */\n+\treadonly commit?: string;\n \treadonly message: string;\n \treadonly time: number;\n }\n@@ -157,10 +159,28 @@\n \t\treadonly publish: Publish | null;\n \t}[];\n }\n+/** One change in a line's past (abso-core issue 20): the line's text after it, and whether it only changed formatting. */\n+export interface TrailStep {\n+\treadonly atom: string;\n+\treadonly author: string;\n+\treadonly message: string;\n+\treadonly time: number;\n+\treadonly text: string;\n+\treadonly formatting: boolean;\n+\treadonly publish: Publish | null;\n+}\n export interface WhyAnswer {\n \treadonly file: string;\n \treadonly line: number;\n+\t/** The version read (null for an agent's view); absent from an older server. */\n+\treadonly version?: number | null;\n \treadonly text: string;\n+\t/** Where \"#N\" in history imported from git points (abso.json's \"gitIssues\"). */\n+\treadonly gitIssues?: string;\n+\t/** The line's changes, newest first, its own first; absent from an older server. */\n+\treadonly trail?: readonly TrailStep[];\n+\t/** The trail went back further than the server follows. */\n+\treadonly trailMore?: true;\n \treadonly change: { readonly atom: string; readonly author: string; readonly message: string; readonly time: number };\n \treadonly publish: Publish | null;\n \treadonly reasons: readonly {"},{"path":"client/src/history.ts","added":74,"removed":6,"diff":"--- a/client/src/history.ts\n+++ b/client/src/history.ts\n@@ -8,7 +8,8 @@\n import type { Env, Output } from \"./commands.ts\";\n import { findRoot, readConfig } from \"./config.ts\";\n import { relativeTo } from \"./folder.ts\";\n-import { byWhom } from \"./history-answers.ts\";\n+import { type GitOrigin, gitOrigin, issueLinks } from \"./git-origin.ts\";\n+import { byWhom, type TrailStep } from \"./history-answers.ts\";\n \n function open(env: Env): { root: string; api: Api; agent: string } {\n \tconst root = findRoot(env.cwd);\n@@ -50,6 +51,26 @@\n \treturn p === null ? \"not pushed\" : `version ${p.version} by ${byWhom(p)} (${when(p.time)}): \"${p.message}\"`;\n }\n \n+/** published, with an imported push's git lines taken off its message (gitLines says them). */\n+function pushed(p: Publish | null, issues: string | undefined): string {\n+\tconst origin = p ? gitOrigin(p, issues) : undefined;\n+\treturn p && origin ? published({ ...p, message: origin.message }) : published(p);\n+}\n+\n+function gitLines(o: GitOrigin): string[] {\n+\treturn [`  From git: commit ${o.commit}${o.author ? ` by ${o.author}` : \"\"}${o.time ? `, ${o.time}` : \"\"}.`];\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 where = s.publish === null ? \"not pushed\" : `version ${s.publish.version}`;\n+\tconst who = s.publish ? byWhom(s.publish) : s.author;\n+\treturn long\n+\t\t? `${where} by ${who} (${when(s.publish?.time ?? s.time)}): \"${subject}\"`\n+\t\t: `${where} (${who}, \"${subject}\")`;\n+}\n+\n export async function runBlame(args: Parsed, env: Env): Promise<Output> {\n \tif (args.positionals.length !== 1)\n \t\tthrow new UsageError(\"blame takes one file: abso blame <file> [--from <n>] [--to <n>]\");\n@@ -72,7 +93,11 @@\n \tfor (const a of b.atoms) {\n \t\tif (a.publish === null) continue;\n \t\tconst own = a.message && a.message !== a.publish.message ? ` (the change itself: \"${a.message}\")` : \"\";\n-\t\tconst line = `  v${a.publish.version} by ${byWhom(a.publish)}, ${when(a.publish.time)}: ${a.publish.message}${own}`;\n+\t\tconst origin = gitOrigin(a.publish);\n+\t\tconst message = origin\n+\t\t\t? `${origin.message.split(\"\\n\")[0]} (from git commit ${origin.commit.slice(0, 12)}${origin.author ? ` by ${origin.author}` : \"\"})`\n+\t\t\t: a.publish.message;\n+\t\tconst line = `  v${a.publish.version} by ${byWhom(a.publish)}, ${when(a.publish.time)}: ${message}${own}`;\n \t\tif (!versions.has(a.publish.version) || own) versions.set(a.publish.version, line);\n \t}\n \tconst whose =\n@@ -112,11 +137,54 @@\n \t\tthrow new UsageError(\"why takes a file and a line: abso why <file>:<line>\");\n \tconst { root, api, agent } = open(env);\n \tconst file = fileArg(root, env.cwd, typed);\n-\tconst w = await api.why(file, lineNumber(raw, \"the line\")!, viewOf(args, agent));\n+\tconst rawAt = str(args, \"at\");\n+\tif (rawAt !== undefined && !/^\\d+$/.test(rawAt))\n+\t\tthrow new UsageError(`--at is a version number (got ${JSON.stringify(rawAt)})`);\n+\tconst at = rawAt === undefined ? undefined : Number(rawAt);\n+\tconst view = viewOf(args, agent);\n+\tif (at !== undefined && \"agent\" in view)\n+\t\tthrow new UsageError(\"--mine reads your view as it is now; it takes no --at\");\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+\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+\tconst skipped = trail.slice(0, leadAt);\n+\tconst earlier = trail.slice(leadAt + 1).filter((s) => all || !s.formatting);\n+\tconst origin = lead?.publish ? gitOrigin(lead.publish, w.gitIssues) : undefined;\n \tconst lines = [\n-\t\t`${w.file} line ${w.line}: ${w.text}`,\n-\t\t`Written by ${w.change.author} (${when(w.change.time)}), pushed in ${published(w.publish)}.`,\n-\t\t...(w.change.message ? [`The change says: \"${w.change.message}\"`] : []),\n+\t\t`${w.file} line ${w.line}${at !== undefined ? ` at version ${at}` : \"\"}: ${w.text}`,\n+\t\t...(lead === undefined || leadAt === 0\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]),\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]\n+\t\t\t: []),\n+\t\t...(earlier.length > 0\n+\t\t\t? [\n+\t\t\t\t\t\"Earlier changes, newest first:\",\n+\t\t\t\t\t...earlier.flatMap((s, i) => {\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];\n+\t\t\t\t\t}),\n+\t\t\t\t]\n+\t\t\t: []),\n+\t\t...(w.trailMore ? [\"  (it goes back further than why follows)\"] : []),\n+\t\t...(w.change.message && leadAt === 0 ? [`The change says: \"${w.change.message}\"`] : []),\n \t\t...(w.settled.length > 0\n \t\t\t? [\n \t\t\t\t\tw.settledBy"},{"path":"client/src/main.ts","added":3,"removed":3,"diff":"--- a/client/src/main.ts\n+++ b/client/src/main.ts\n@@ -732,10 +732,10 @@\n \t\trun: runBlame,\n \t},\n \twhy: {\n-\t\tflags: { draft: \"string\", mine: \"boolean\" },\n-\t\tusage: \"abso why <file>:<line> [--draft <name> | --mine]\",\n+\t\tflags: { draft: \"string\", mine: \"boolean\", at: \"string\", all: \"boolean\" },\n+\t\tusage: \"abso why <file>:<line> [--at <version>] [--all] [--draft <name> | --mine]\",\n \t\tabout:\n-\t\t\t\"Why one line of shared (or of a named draft, or your own view) is the way it is: its change, its push, its reasons, and any collision it settled.\",\n+\t\t\t\"Why one line of shared (or of a named draft, or your own view) is the way it is: the newest change to what it does, its push, its reasons, any collision it settled, and the line's earlier changes, newest first. Changes that only touched formatting (whitespace, line wrapping, quotes, trailing commas) are skipped, and why says which; --all shows them too. A line imported from git names its commit, and #N in it names the old repository's issue when abso.json's \\\"gitIssues\\\" says where those are. --at <version> reads the line as shared (or the draft) had it then, as blame --version does.\",\n \t\trun: runWhy,\n \t},\n \tdrop: {"},{"path":"client/test/fake-server.ts","added":1,"removed":1,"diff":"--- a/client/test/fake-server.ts\n+++ b/client/test/fake-server.ts\n@@ -434,7 +434,7 @@\n \t\t\t\t\treturn send(res, 200, blame(hosted.project, q.file, num(q.from), num(q.to), num(q.version), view));\n \t\t\t\tconst line = num(q.line);\n \t\t\t\tif (!line || !Number.isInteger(line)) return send(res, 400, { error: \"why needs ?line=<number>\" });\n-\t\t\t\treturn send(res, 200, why(hosted.project, q.file, line, view));\n+\t\t\t\treturn send(res, 200, why(hosted.project, q.file, line, { ...view, version: num(q.version) }));\n \t\t\t} catch (e) {\n \t\t\t\tconst code = (e as { code?: string }).code;\n \t\t\t\treturn send(res, code === \"bad-request\" ? 400 : 500, { error: e instanceof Error ? e.message : String(e) });"},{"path":"client/test/why-trail.test.ts","added":144,"removed":0,"diff":"--- /dev/null\n+++ b/client/test/why-trail.test.ts\n@@ -0,0 +1,144 @@\n+// abso why past formatting to the reason (abso-core issue 20), and lines\n+// imported from git naming their commit and their tracker (issue 21), from\n+// the command line: a line imported from git, then reformatted.\n+\n+import assert from \"node:assert/strict\";\n+import { mkdtempSync, readFileSync, rmSync, writeFileSync } from \"node:fs\";\n+import { tmpdir } from \"node:os\";\n+import { join } from \"node:path\";\n+import { after, before, describe, it } from \"node:test\";\n+import { gitOrigin, issueLinks, issueNumbers, issuesOf } from \"../src/git-origin.ts\";\n+import { main } from \"../src/main.ts\";\n+import { type Fake, startFake } from \"./fake-server.ts\";\n+\n+const TOKEN = \"test-token-not-secret\";\n+const ENV = { AVCS_TOKEN: TOKEN, AVCS_HOOK_FORCE: \"1\", AVCS_SERVERS_FILE: \"no-servers.json\" };\n+const IMPORTED =\n+\t\"Merge queue: check publishes in batches (#39)\\n\\nCap the batch at 8.\\n\\ngit 4e1a7c2b9f03, Ada Lovelace, 2026-10-06T20:36:43-05:00\";\n+\n+let fake: Fake;\n+const dirs: string[] = [];\n+before(async () => {\n+\tfake = await startFake(0, TOKEN);\n+});\n+after(async () => {\n+\tawait fake.close();\n+\tfor (const d of dirs) rmSync(d, { recursive: true, force: true, maxRetries: 5 });\n+});\n+\n+async function run(cwd: string, ...argv: string[]): Promise<{ exit: number; out: string }> {\n+\tconst r = await main(argv, { cwd, env: ENV, write: () => {} });\n+\treturn { exit: r.exit, out: r.stdout + r.stderr };\n+}\n+\n+let projects = 0;\n+/** v0 init; v1 a git import changes the cap line; v2 a reformat. */\n+async function history(config = '{ \"check\": \"true\" }\\n', message = IMPORTED): Promise<string> {\n+\tconst dir = mkdtempSync(join(tmpdir(), \"abso-why-\"));\n+\tdirs.push(dir);\n+\twriteFileSync(join(dir, \"abso.json\"), config);\n+\twriteFileSync(join(dir, \"queue.ts\"), \"export function take(w) {\\n\\tconst n = Math.min(4, w);\\n\\treturn n;\\n}\\n\");\n+\tconst made = await run(\n+\t\tdir,\n+\t\t\"init\",\n+\t\t\"--server\",\n+\t\tfake.url,\n+\t\t\"--project\",\n+\t\t`why${++projects}`,\n+\t\t\"--agent\",\n+\t\t\"imp\",\n+\t\t\"--as-is\",\n+\t);\n+\tassert.equal(made.exit, 0, made.out);\n+\tconst edit = (from: string, to: string) =>\n+\t\twriteFileSync(join(dir, \"queue.ts\"), readFileSync(join(dir, \"queue.ts\"), \"utf8\").replace(from, to));\n+\tedit(\"Math.min(4, w); \", \"Math.min(4, w);\");\n+\tedit(\"Math.min(4, w);\", \"Math.min(8, w); // #39\");\n+\tlet r = await run(dir, \"push\", \"-m\", message);\n+\tassert.equal(r.exit, 0, r.out);\n+\tedit(\"\\tconst n = Math.min(8, w); // #39\", \"  const n = Math.min(8, w) // #39\");\n+\tr = await run(dir, \"push\", \"-m\", \"Format the code\");\n+\tassert.equal(r.exit, 0, r.out);\n+\treturn dir;\n+}\n+\n+describe(\"abso why\", () => {\n+\tit(\"leads with the change that said what the line does, and says which formatting change it skipped\", async () => {\n+\t\tconst dir = await history('{ \"gitIssues\": \"https://github.com/owner/repo/issues/\" }\\n');\n+\t\tconst r = await run(dir, \"why\", \"queue.ts:2\");\n+\t\tassert.equal(r.exit, 0, r.out);\n+\t\tassert.match(r.out, /^queue\\.ts line 2: {3}const n = Math\\.min\\(8, w\\) \\/\\/ #39$/m);\n+\t\tassert.match(r.out, /^Written by imp \\(.*\\), pushed in version 1 by imp \\(.*\\): \"Merge queue: check publishes/m);\n+\t\tassert.doesNotMatch(r.out, /pushed in version 1 [^\\n]*git d9cf/, \"the git line is said once, below\");\n+\t\tassert.match(r.out, /^ {2}From git: commit 4e1a7c2b9f03 by Ada Lovelace, 2026-10-06T20:36:43-05:00\\.$/m);\n+\t\tassert.match(r.out, /^ {2}#39 is https:\\/\\/github\\.com\\/owner\\/repo\\/issues\\/39$/m);\n+\t\tassert.match(r.out, /^Skipped 1 formatting change since: version 2 \\(imp, \"Format the code\"\\)\\./m);\n+\t\tassert.match(\n+\t\t\tr.out,\n+\t\t\t/^Earlier changes, newest first:\\n {2}version 0 by init .*\"Start the project\"\\n {4}It read: const n = Math\\.min\\(4, w\\);$/m,\n+\t\t);\n+\t});\n+\n+\tit(\"--all leads with the newest change, formatting or not; --at reads an older version\", async () => {\n+\t\tconst dir = await history();\n+\t\tconst all = await run(dir, \"why\", \"queue.ts:2\", \"--all\");\n+\t\tassert.equal(all.exit, 0, all.out);\n+\t\tassert.match(all.out, /^Written by imp .*pushed in version 2 .*\"Format the code\"/m);\n+\t\tassert.match(\n+\t\t\tall.out,\n+\t\t\t/^ {2}version 1 by imp .*\"Merge queue: check publishes in batches \\(#39\\)\", from git commit 4e1a7c2b9f03 by Ada Lovelace$/m,\n+\t\t);\n+\t\tassert.doesNotMatch(all.out, /#39 is/, \"no tracker: abso.json doesn't say where\");\n+\t\tconst at = await run(dir, \"why\", \"queue.ts:2\", \"--at\", \"0\", \"--json\");\n+\t\tassert.equal(at.exit, 0, at.out);\n+\t\tassert.equal(JSON.parse(at.out).text, \"\\tconst n = Math.min(4, w);\");\n+\t});\n+\n+\tit(\"blame names an imported push's commit\", async () => {\n+\t\tconst dir = await history();\n+\t\tconst r = await run(dir, \"blame\", \"queue.ts\", \"--version\", \"1\");\n+\t\tassert.equal(r.exit, 0, r.out);\n+\t\tassert.match(\n+\t\t\tr.out,\n+\t\t\t/^ {2}v1 by imp, .*: Merge queue: check publishes in batches \\(#39\\) \\(from git commit 4e1a7c2b9f03 by Ada Lovelace\\)$/m,\n+\t\t);\n+\t});\n+});\n+\n+describe(\"git origins\", () => {\n+\tit(\"reads import-git's lines, a git push's, or the host's commit\", () => {\n+\t\tassert.deepEqual(gitOrigin({ message: `${IMPORTED}\\nIssues: https://github.com/o/r/issues` }), {\n+\t\t\tcommit: \"4e1a7c2b9f03\",\n+\t\t\tauthor: \"Ada Lovelace\",\n+\t\t\ttime: \"2026-10-06T20:36:43-05:00\",\n+\t\t\tissues: \"https://github.com/o/r/issues\",\n+\t\t\tmessage: \"Merge queue: check publishes in batches (#39)\\n\\nCap the batch at 8.\",\n+\t\t});\n+\t\tassert.deepEqual(gitOrigin({ message: \"Fix\\n\\nGit-Commit: abcdef1234\\nAuthor: Bo\" }), {\n+\t\t\tcommit: \"abcdef1234\",\n+\t\t\tauthor: \"Bo\",\n+\t\t\tmessage: \"Fix\",\n+\t\t});\n+\t\tassert.deepEqual(gitOrigin({ message: \"Plain\", commit: \"abc1234\", author: \"Cy\" }, \"https://x/issues\"), {\n+\t\t\tcommit: \"abc1234\",\n+\t\t\tauthor: \"Cy\",\n+\t\t\tissues: \"https://x/issues\",\n+\t\t\tmessage: \"Plain\",\n+\t\t});\n+\t\tassert.equal(gitOrigin({ message: \"Written in abso (#39)\" }), undefined);\n+\t});\n+\n+\tit(\"names each #N once, and leaves owner/repo#N alone\", () => {\n+\t\tassert.deepEqual(issueNumbers(\"see #39 and #4, again #39; other/repo#7, a#5\"), [39, 4]);\n+\t\tassert.deepEqual(issueLinks(\"(#39)\", \"https://g/o/r/issues\"), [\"#39 is https://g/o/r/issues/39\"]);\n+\t\tassert.deepEqual(issueLinks(\"(#39)\", undefined), []);\n+\t});\n+\n+\tit(\"finds a remote's issues on GitHub or GitLab, by https or ssh\", () => {\n+\t\tassert.equal(issuesOf(\"https://github.com/o/r.git\"), \"https://github.com/o/r/issues\");\n+\t\tassert.equal(issuesOf(\"git@github.com:o/r.git\"), \"https://github.com/o/r/issues\");\n+\t\tassert.equal(issuesOf(\"https://gitlab.com/g/sub/r\"), \"https://gitlab.com/g/sub/r/-/issues\");\n+\t\tassert.equal(issuesOf(\"https://example.com/o/r.git\"), undefined);\n+\t\tassert.equal(issuesOf(\"\"), undefined);\n+\t});\n+});"},{"path":"server/src/engine-project.ts","added":6,"removed":2,"diff":"--- a/server/src/engine-project.ts\n+++ b/server/src/engine-project.ts\n@@ -496,8 +496,12 @@\n \t\t\t...(folder !== undefined ? { folder } : {}),\n \t\t});\n \t}\n-\twhy(file: string, line: number, draft?: string, agent?: string) {\n-\t\treturn why(this.project, file, line, { ...(draft ? { draft } : {}), ...(agent ? { agent } : {}) });\n+\twhy(file: string, line: number, draft?: string, agent?: string, version?: number) {\n+\t\treturn why(this.project, file, line, {\n+\t\t\t...(draft ? { draft } : {}),\n+\t\t\t...(agent ? { agent } : {}),\n+\t\t\t...(version !== undefined ? { version } : {}),\n+\t\t});\n \t}\n \twhere() {\n \t\treturn where(this.project);"},{"path":"worker/project/blame.ts","added":63,"removed":1,"diff":"--- a/worker/project/blame.ts\n+++ b/worker/project/blame.ts\n@@ -2,11 +2,14 @@\n // A line's id names the atom that inserted it, so blame is a lookup per line:\n // that change's author and message, and the publish that brought it into\n // shared, with its message. Why adds the change's reasons, what it settled,\n-// and the reasons other changes give that point at it.\n+// and the reasons other changes give that point at it, and the line's\n+// earlier changes (trail.ts).\n import { type Atom, type AtomId, type LineId, type ReadView, View } from \"../../src/engine/index.ts\";\n import { blocksIn, isMarkerLine } from \"./markers.ts\";\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 \n export interface BlameLine {\n \tn: number;\n@@ -225,11 +228,14 @@\n \t\tsettled.length > 0\n \t\t\t? heldPush(project, settle, file, Math.max(0, ...settled.map((s) => s.publish?.time ?? 0)))\n \t\t\t: undefined;\n+\tconst gitIssues = gitIssuesOf(project.sharedView());\n \treturn {\n \t\tfile,\n \t\tline: n,\n+\t\tversion: where.agent !== undefined ? null : (where.version ?? project.lastVersion(where.draft ?? null)),\n \t\ttext: line.text,\n \t\tid: line.id,\n+\t\t...(gitIssues !== undefined ? { gitIssues } : {}),\n \t\tchange: {\n \t\t\tatom: atom.id,\n \t\t\tauthor: atom.author,\n@@ -245,10 +251,66 @@\n \t\t...(kept ? { settledBy: { atom: kept.id, author: kept.author, publish: publishOf(project, kept.id) } } : {}),\n \t\t...(held ? { held } : {}),\n \t\tbuiltOn,\n+\t\t...lineTrail(project, view, file, line.id),\n \t};\n }\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+ * 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+\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\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\tpublish,\n+\t\t});\n+\t}\n+\treturn { trail: out, ...(more ? { trailMore: true as const } : {}) };\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);\n+\tif (!file) return undefined;\n+\ttry {\n+\t\tconst url = (JSON.parse(file.text) as { gitIssues?: unknown }).gitIssues;\n+\t\treturn typeof url === \"string\" ? url : undefined;\n+\t} catch {\n+\t\treturn undefined;\n+\t}\n+}\n+\n+function samePublish(a: Publish | null, b: Publish | null): boolean {\n+\treturn a === null || b === null ? a === b : a.draft === b.draft && a.version === b.version;\n+}\n+\n+/**\n  * Who wrote a folder (#41, F33): every line of every file under it (the\n  * whole project for \"\"), counted by the agent that wrote it, most first.\n  * Binary files and files kept whole have no lines, so they count as files"},{"path":"worker/project/http.ts","added":1,"removed":1,"diff":"--- a/worker/project/http.ts\n+++ b/worker/project/http.ts\n@@ -316,7 +316,7 @@\n \t\t\t\treturn { status: 200, body: history.blame(q.file, num(q.from), num(q.to), num(q.version), draft, who) };\n \t\t\tconst line = num(q.line);\n \t\t\tif (!line || !Number.isInteger(line)) return fail(400, \"why needs ?line=<number>\");\n-\t\t\treturn { status: 200, body: history.why(q.file, line, draft, who) };\n+\t\t\treturn { status: 200, body: history.why(q.file, line, draft, who, num(q.version)) };\n \t\t}\n \t\tif (op === \"announce\" && agent === undefined) {\n \t\t\t// The sender comes from the route, like every other agent op, not from"},{"path":"worker/project/project-api.ts","added":1,"removed":1,"diff":"--- a/worker/project/project-api.ts\n+++ b/worker/project/project-api.ts\n@@ -102,7 +102,7 @@\n export interface ProjectHistory {\n \t/** draft: a named draft's view (at version); agent: an agent's view now, its unpublished changes included. */\n \tblame(file: string, from?: number, to?: number, version?: number, draft?: string, agent?: string): unknown;\n-\twhy(file: string, line: number, draft?: string, agent?: string): unknown;\n+\twhy(file: string, line: number, draft?: string, agent?: string, version?: number): unknown;\n \t/** Who wrote a folder: its lines counted by author (#41, F33). */\n \tfolderBlame(folder: string, version?: number, draft?: string, agent?: string): unknown;\n \t/** One folder's entries with each one's last change (#41, F2, F34). */"},{"path":"worker/project/project-helpers.ts","added":4,"removed":1,"diff":"--- a/worker/project/project-helpers.ts\n+++ b/worker/project/project-helpers.ts\n@@ -58,7 +58,8 @@\n  * What's wrong with a project file (abso.json or avcs.json), or undefined:\n  * it must be a JSON object, and the keys abso reads must have their types:\n  * \"whole\" a list of globs, \"queue\" true or false, \"review\" a list of\n- * {paths, reviewer}, and CI's \"setup\", \"build\", \"check\" and \"out\" strings.\n+ * {paths, reviewer}, CI's \"setup\", \"build\", \"check\" and \"out\" strings, and\n+ * \"gitIssues\" (where \"#N\" in history imported from git points) a web address.\n  */\n export function configProblem(text: string): string | undefined {\n \tlet config: unknown;\n@@ -86,6 +87,8 @@\n \t\t\t});\n \t\tif (!ok) return `has \"review\" that isn't a list of {\"paths\": [globs], \"reviewer\": \"<agent>\"}`;\n \t}\n+\tif (c.gitIssues !== undefined && (typeof c.gitIssues !== \"string\" || !/^https?:\\/\\/\\S+$/.test(c.gitIssues)))\n+\t\treturn `has \"gitIssues\" that isn't a web address (like \"https://github.com/owner/repo/issues\")`;\n \tfor (const key of [\"setup\", \"build\", \"check\", \"out\"])\n \t\tif (c[key] !== undefined && typeof c[key] !== \"string\") return `has \"${key}\" that isn't a string`;\n \treturn undefined;"},{"path":"worker/project/test/trail.test.ts","added":98,"removed":0,"diff":"--- /dev/null\n+++ b/worker/project/test/trail.test.ts\n@@ -0,0 +1,98 @@\n+// A line's earlier changes (abso-core issue 20): why follows a line back\n+// through the changes that rewrote it, and marks the ones that only changed\n+// its formatting.\n+\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { why } from \"../blame.ts\";\n+import { MemoryStore, Project } from \"../project.ts\";\n+import { normalize, pairIn, trail } from \"../trail.ts\";\n+\n+function setup(files: Record<string, string>) {\n+\tconst project = new Project(new MemoryStore());\n+\tproject.init(files, 0);\n+\tfor (const a of [\"ann\", \"bob\", \"fmt\"]) project.sync(a);\n+\treturn project;\n+}\n+\n+function push(p: Project, agent: string, file: string, old: string, now: string, message: string, time: number) {\n+\tp.sync(agent);\n+\tp.edit(agent, { file, old, new: now }, time);\n+\treturn p.publish(agent, message, time + 1);\n+}\n+\n+test(\"normalized text ignores whitespace, wrapping, quotes, trailing commas, line-end semicolons and lone arrow parentheses\", () => {\n+\tassert.equal(normalize([\"const a = f('x', 1);\"]), normalize(['const a = f(\"x\", 1)']));\n+\tassert.equal(normalize([\"f(a, b)\"]), normalize([\"f(\", \"\\ta,\", \"\\tb,\", \")\"]));\n+\tassert.equal(normalize([\"xs.map((x) => x)\"]), normalize([\"xs.map(x => x)\"]));\n+\tassert.notEqual(normalize([\"const n = 8;\"]), normalize([\"const n = 9;\"]));\n+});\n+\n+test(\"pairing: a rewrapped run pairs by its characters, a real edit by similarity, a new line with nothing\", () => {\n+\tassert.deepEqual(pairIn([\"f(a, b);\"], [\"f(\", \"  a,\", \"  b,\", \");\"], 1), { index: 0, formatting: true });\n+\tassert.deepEqual(pairIn([\"const n = Math.min(8, w);\"], [\"const n = Math.min(cap ?? 8, w);\"], 0), {\n+\t\tindex: 0,\n+\t\tformatting: false,\n+\t});\n+\tassert.equal(pairIn([\"const n = 8;\"], [\"// nothing like it at all, really\"], 0), undefined);\n+\tassert.equal(pairIn([], [\"new\"], 0), undefined);\n+});\n+\n+test(\"why follows a line back past a reformat to the change that said what it does\", () => {\n+\tconst p = setup({ \"q.ts\": \"export function take(w: number) {\\n\\tconst n = Math.min(8, w);\\n\\treturn n;\\n}\\n\" });\n+\tpush(p, \"ann\", \"q.ts\", \"Math.min(8, w)\", \"Math.min(cap ?? 8, w)\", \"Cap the batch at 8, tunable per project\", 10); // v1\n+\tpush(p, \"bob\", \"q.ts\", \"\\treturn n;\\n\", \"\\treturn n; // the count\\n\", \"Say what n is\", 20); // v2, beside it\n+\tpush(\n+\t\tp,\n+\t\t\"fmt\",\n+\t\t\"q.ts\",\n+\t\t\"\\tconst n = Math.min(cap ?? 8, w);\\n\",\n+\t\t\"    const n = Math.min(cap ?? 8, w)\\n\",\n+\t\t\"Reformat\",\n+\t\t30,\n+\t); // v3\n+\n+\tconst w = why(p, \"q.ts\", 2);\n+\tassert.equal(w.text, \"    const n = Math.min(cap ?? 8, w)\");\n+\tassert.deepEqual(\n+\t\tw.trail.map((s) => [s.publish?.version, s.formatting, s.text]),\n+\t\t[\n+\t\t\t[3, true, \"    const n = Math.min(cap ?? 8, w)\"],\n+\t\t\t[1, false, \"\\tconst n = Math.min(cap ?? 8, w);\"],\n+\t\t\t[0, false, \"\\tconst n = Math.min(8, w);\"],\n+\t\t],\n+\t);\n+\tassert.equal(w.trail[1]!.publish?.message, \"Cap the batch at 8, tunable per project\");\n+\tassert.equal(w.trailMore, undefined);\n+});\n+\n+test(\"a reformat that wraps one line onto several leads each piece back to it\", () => {\n+\tconst p = setup({ \"a.ts\": \"call(alpha, beta);\\n\" });\n+\tpush(p, \"fmt\", \"a.ts\", \"call(alpha, beta);\\n\", \"call(\\n\\talpha,\\n\\tbeta,\\n);\\n\", \"Wrap\", 10); // v1\n+\tconst w = why(p, \"a.ts\", 3);\n+\tassert.deepEqual(\n+\t\tw.trail.map((s) => [s.publish?.version, s.formatting, s.text]),\n+\t\t[\n+\t\t\t[1, true, \"\\tbeta,\"],\n+\t\t\t[0, false, \"call(alpha, beta);\"],\n+\t\t],\n+\t);\n+});\n+\n+test(\"why --at reads the line as shared had it at that version\", () => {\n+\tconst p = setup({ \"a.ts\": \"one\\ntwo\\n\" });\n+\tpush(p, \"ann\", \"a.ts\", \"one\\n\", \"zero\\none\\n\", \"Add zero\", 10); // v1\n+\tassert.equal(why(p, \"a.ts\", 1, { version: 0 }).text, \"one\");\n+\tassert.equal(why(p, \"a.ts\", 2).text, \"one\");\n+});\n+\n+test(\"the trail stops after the most steps it may take, and says so\", () => {\n+\tconst p = setup({ \"a.ts\": \"v = 0\\n\" });\n+\tfor (let i = 1; i <= 4; i++) push(p, \"ann\", \"a.ts\", `v = ${i - 1}`, `v = ${i}`, `to ${i}`, i * 10);\n+\tconst view = p.sharedView();\n+\tconst line = view.lines(\"a.ts\")[0]!.id;\n+\tconst t = trail(p.graph, view, \"a.ts\", line, 2);\n+\tassert.equal(t.steps.length, 2);\n+\tassert.equal(t.more, true);\n+\tassert.equal(trail(p.graph, view, \"a.ts\", line).steps.length, 5);\n+});"},{"path":"worker/project/trail.ts","added":240,"removed":0,"diff":"--- /dev/null\n+++ b/worker/project/trail.ts\n@@ -0,0 +1,240 @@\n+// A line's earlier changes, for why (abso-core issue 20). A line's id names\n+// the change that wrote it, so a line that was rewritten (reformatted,\n+// renamed, edited) is a new line, and the one it replaced is a deleted line\n+// in the same file. The file's full sequence keeps deleted lines where they\n+// were, so the lines a change deleted and the lines it wrote sit together:\n+// that's one hunk, and the line it replaced is the one it pairs with there.\n+// Following those pairs back gives the line's changes, newest first. A\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+\n+import type { Atom, AtomId, Graph, LineId, ReadView, SeqLine } from \"../../src/engine/index.ts\";\n+\n+/** One change in a line's past, newest first. */\n+export interface TrailStep {\n+\t/** The change that wrote the line. */\n+\treadonly atom: AtomId;\n+\t/** The line it wrote, and its text then. */\n+\treadonly line: LineId;\n+\treadonly text: string;\n+\t/** It only changed formatting from the line it replaced. */\n+\treadonly formatting: boolean;\n+\t/** The line it replaced, when it replaced one. */\n+\treadonly replaced?: LineId;\n+}\n+\n+/** At most this many steps back. */\n+export const TRAIL_MAX = 50;\n+\n+/**\n+ * Text with formatting taken out, for comparing two versions of the same\n+ * code: no whitespace, one kind of quote, no trailing commas before a\n+ * closing bracket, no semicolons at line ends (or before a comment there), and no parentheses around a\n+ * lone arrow parameter. Lines are normalized then joined, so a statement\n+ * wrapped onto three lines matches it on one.\n+ */\n+export function normalize(lines: readonly string[]): string {\n+\treturn lines\n+\t\t.map((l) => l.replace(/;\\s*(\\/\\/.*)?$/, \"$1\"))\n+\t\t.join(\"\")\n+\t\t.replace(/\\s+/g, \"\")\n+\t\t.replace(/['`]/g, '\"')\n+\t\t.replace(/,(?=[)\\]}]|$)/g, \"\")\n+\t\t.replace(/\\(([A-Za-z_$][\\w$]*)\\)=>/g, \"$1=>\");\n+}\n+\n+/** Whether two runs of lines are the same code, formatted differently (or not at all). */\n+export function sameCode(before: readonly string[], after: readonly string[]): boolean {\n+\treturn normalize(before) === normalize(after);\n+}\n+\n+/** Similarity of two strings, 0 to 1: shared character pairs (Dice). */\n+function similarity(a: string, b: string): number {\n+\tif (a === b) return 1;\n+\tif (a.length < 2 || b.length < 2) return 0;\n+\tconst pairs = new Map<string, number>();\n+\tfor (let i = 0; i < a.length - 1; i++) {\n+\t\tconst p = a.slice(i, i + 2);\n+\t\tpairs.set(p, (pairs.get(p) ?? 0) + 1);\n+\t}\n+\tlet shared = 0;\n+\tfor (let i = 0; i < b.length - 1; i++) {\n+\t\tconst p = b.slice(i, i + 2);\n+\t\tconst n = pairs.get(p) ?? 0;\n+\t\tif (n > 0) {\n+\t\t\tshared++;\n+\t\t\tpairs.set(p, n - 1);\n+\t\t}\n+\t}\n+\treturn (2 * shared) / (a.length + b.length - 2);\n+}\n+\n+/** Below this, a rewritten line isn't taken for the one it replaced: it was new. */\n+const SIMILAR = 0.4;\n+\n+/**\n+ * For a run that's the same code reformatted: which old line each new line\n+ * came from, by where its characters fall in the joined, normalized text\n+ * (the old line it overlaps most).\n+ */\n+function byCharacters(before: readonly string[], after: readonly string[], at: number): number | undefined {\n+\tconst spans = (lines: readonly string[]) => {\n+\t\tlet from = 0;\n+\t\treturn lines.map((l) => {\n+\t\t\tconst n = normalize([l]).length;\n+\t\t\tconst span = [from, from + n] as const;\n+\t\t\tfrom += n;\n+\t\t\treturn span;\n+\t\t});\n+\t};\n+\tconst [from, to] = spans(after)[at]!;\n+\tlet best: number | undefined;\n+\tlet most = 0;\n+\tfor (const [i, [a, b]] of spans(before).entries()) {\n+\t\tconst overlap = Math.min(to, b) - Math.max(from, a);\n+\t\tif (overlap > most) [best, most] = [i, overlap];\n+\t}\n+\treturn best;\n+}\n+\n+/** Longest common subsequence of two lists of keys: matched index pairs, in order. */\n+function common(a: readonly string[], b: readonly string[]): [number, number][] {\n+\tconst n = a.length;\n+\tconst m = b.length;\n+\t// Hunks are small; a full table is fine, but cap it so a whole-file rewrite stays cheap.\n+\tif (n * m > 4_000_000) return [];\n+\tconst t: Uint32Array[] = Array.from({ length: n + 1 }, () => new Uint32Array(m + 1));\n+\tfor (let i = n - 1; i >= 0; i--)\n+\t\tfor (let j = m - 1; j >= 0; j--)\n+\t\t\tt[i]![j] = a[i] === b[j] ? t[i + 1]![j + 1]! + 1 : Math.max(t[i + 1]![j]!, t[i]![j + 1]!);\n+\tconst out: [number, number][] = [];\n+\tlet i = 0;\n+\tlet j = 0;\n+\twhile (i < n && j < m) {\n+\t\tif (a[i] === b[j]) {\n+\t\t\tout.push([i, j]);\n+\t\t\ti++;\n+\t\t\tj++;\n+\t\t} else if (t[i + 1]![j]! >= t[i]![j + 1]!) i++;\n+\t\telse j++;\n+\t}\n+\treturn out;\n+}\n+\n+/**\n+ * In one hunk, the old line a new one replaced, and whether that was only\n+ * formatting. Lines whose normalized text matches pair first; between those,\n+ * a run that's the same code rewrapped pairs by its characters; anything\n+ * else pairs with the most similar old line, if one is similar enough.\n+ */\n+export function pairIn(\n+\tbefore: readonly string[],\n+\tafter: readonly string[],\n+\tat: number,\n+): { index: number; formatting: boolean } | undefined {\n+\tif (before.length === 0) return undefined;\n+\tif (sameCode(before, after)) {\n+\t\tconst index = byCharacters(before, after, at);\n+\t\treturn index === undefined ? undefined : { index, formatting: true };\n+\t}\n+\tconst keys = (lines: readonly string[]) => lines.map((l) => normalize([l]));\n+\tconst a = keys(before);\n+\tconst b = keys(after);\n+\tconst anchors = common(a, b);\n+\tconst hit = anchors.find(([, j]) => j === at);\n+\tif (hit && a[hit[0]] !== \"\") return { index: hit[0], formatting: true };\n+\t// The gap between anchors that holds the new line.\n+\tlet [i0, j0] = [0, 0];\n+\tlet [i1, j1] = [before.length, after.length];\n+\tfor (const [i, j] of anchors) {\n+\t\tif (j < at) [i0, j0] = [i + 1, j + 1];\n+\t\telse if (j > at) {\n+\t\t\t[i1, j1] = [i, j];\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\tconst gapBefore = before.slice(i0, i1);\n+\tconst gapAfter = after.slice(j0, j1);\n+\tif (gapBefore.length === 0) return undefined;\n+\tif (sameCode(gapBefore, gapAfter)) {\n+\t\tconst index = byCharacters(gapBefore, gapAfter, at - j0);\n+\t\treturn index === undefined ? undefined : { index: i0 + index, formatting: true };\n+\t}\n+\tlet best: number | undefined;\n+\tlet most = SIMILAR;\n+\tfor (let i = i0; i < i1; i++) {\n+\t\tconst s = similarity(a[i]!, b[at]!);\n+\t\tif (s >= most) [best, most] = [i, s];\n+\t}\n+\treturn best === undefined ? undefined : { index: best, formatting: false };\n+}\n+\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+ */\n+export function trail(\n+\tgraph: Graph,\n+\tview: ReadView,\n+\tfile: string,\n+\tline: LineId,\n+\tmax = TRAIL_MAX,\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 steps: TrailStep[] = [];\n+\tlet current: LineId | undefined = line;\n+\twhile (current !== undefined && steps.length < max) {\n+\t\tconst at = index.get(current);\n+\t\tif (at === undefined) break;\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\tsteps.push({\n+\t\t\tatom: atom.id,\n+\t\t\tline: current,\n+\t\t\ttext: seq[at]!.text,\n+\t\t\tformatting: pair?.formatting ?? false,\n+\t\t\t...(replaced !== undefined ? { replaced } : {}),\n+\t\t});\n+\t\tcurrent = replaced;\n+\t}\n+\treturn { steps, more: current !== 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\n+ * deleted) are passed over; a line it left alone ends the hunk.\n+ */\n+function hunkOf(graph: Graph, seq: readonly SeqLine[], at: number, atom: Atom): { before: number[]; after: number[] } {\n+\tconst seqOf = (id: AtomId) => graph.atom(id).seq;\n+\tconst role = (s: SeqLine): \"wrote\" | \"deleted\" | \"unseen\" | \"kept\" => {\n+\t\tif (s.atom === atom.id) return \"wrote\";\n+\t\tif (seqOf(s.atom) > atom.seq) return \"unseen\";\n+\t\tif (s.deletedBy.some((d) => d !== atom.id && seqOf(d) < atom.seq)) return \"unseen\";\n+\t\treturn s.deletedBy.includes(atom.id) ? \"deleted\" : \"kept\";\n+\t};\n+\tlet first = at;\n+\twhile (first > 0 && role(seq[first - 1]!) !== \"kept\") first--;\n+\tlet last = at;\n+\twhile (last < seq.length - 1 && role(seq[last + 1]!) !== \"kept\") last++;\n+\tconst before: number[] = [];\n+\tconst after: number[] = [];\n+\tfor (let i = first; i <= last; i++) {\n+\t\tconst r = role(seq[i]!);\n+\t\tif (r === \"deleted\") before.push(i);\n+\t\telse if (r === \"wrote\") after.push(i);\n+\t}\n+\treturn { before, after };\n+}"}],"ci":null}