{"draft":null,"from":48,"to":318,"files":[{"path":"worker/project/version-files.ts","added":272,"removed":0,"diff":"--- /dev/null\n+++ b/worker/project/version-files.ts\n@@ -0,0 +1,272 @@\n+// Each version of shared's changed files, worked out as the version lands from what it touched (disk-first 2;\n+// abso Capacity). The log, a path's history and the tree read these lists; working them out from two layouts of every\n+// touched file per version cost game's whole history 460 s to adopt. Here a version costs about its own size:\n+// - a file it touched differs when its visible lines' count or texts changed, or its mode or blob did;\n+// - whether a file exists before and after comes from a running count of each file's visible lines;\n+// - only a version whose added and removed lines have the very same texts, or one with moves, drops or a change to\n+//   shared's closure, is laid out as before.\n+// The answers are exactly differs() in view-changes.ts.\n+\n+import { type AtomId, type Graph, type ReadView, View } from \"../../src/engine/index.ts\";\n+import type { GraphStateStorage } from \"../../src/engine/state-source.ts\";\n+import { touchedFiles } from \"./collisions.ts\";\n+import { utf8Bytes } from \"./project-helpers.ts\";\n+import type { Published } from \"./spaces.ts\";\n+import { differs } from \"./view-changes.ts\";\n+\n+const COUNTS = \"head-visible\";\n+const META = \"head-visible:meta\";\n+\n+// Author totals (docs/disk-first-2.md, abso Capacity, issue 664): each shared file's lines by author and its text's size\n+// in UTF-8 bytes, as tree with authors counts them, so it reads one value per file instead of laying every file out.\n+// AUTHORS holds each file's totals at the newest version kept; AUTHORS_LOG each file's totals at every version that\n+// changed them, oldest first. Shared only, never a draft (Security on ask 2096). They live in the writer's state, so\n+// whatever clears or replaces that state (a new adoption, a restore, a drop of the project) takes them with it.\n+const AUTHORS = \"head-authors\";\n+const AUTHORS_LOG = \"author-totals\";\n+const AUTHORS_META = \"head-authors:meta\";\n+\n+/** A file's lines by author and its text's size, stored: authors by name, none at zero; null when it has no lines. */\n+type StoredTotals = { a: [string, number][]; b: number };\n+\n+/** A shared file's lines by author and its text's size in UTF-8 bytes. */\n+export interface FileTotals {\n+\treadonly authors: ReadonlyMap<string, number>;\n+\treadonly bytes: number;\n+}\n+\n+const NO_TOTALS: FileTotals = { authors: new Map(), bytes: 0 };\n+\n+/** The newest shared version whose author totals are kept, or undefined when this writer keeps none. */\n+export function authoredVersion(storage: GraphStateStorage): number | undefined {\n+\treturn storage.read(AUTHORS_META, \"version\") as number | undefined;\n+}\n+\n+/** A shared file's totals at version `v`, or undefined when they aren't kept that far: lay the file out instead. */\n+export function authorTotalsAt(storage: GraphStateStorage, path: string, v: number): FileTotals | undefined {\n+\tconst upTo = authoredVersion(storage);\n+\tif (upTo === undefined || v > upTo) return undefined;\n+\tconst log = storage.read(AUTHORS_LOG, path) as [number, StoredTotals | null][] | undefined;\n+\tif (!log) return NO_TOTALS;\n+\t// The newest entry at or before v.\n+\tlet lo = 0;\n+\tlet hi = log.length - 1;\n+\tlet found = -1;\n+\twhile (lo <= hi) {\n+\t\tconst mid = (lo + hi) >> 1;\n+\t\tif (log[mid]![0] <= v) {\n+\t\t\tfound = mid;\n+\t\t\tlo = mid + 1;\n+\t\t} else hi = mid - 1;\n+\t}\n+\tconst totals = found < 0 ? null : log[found]![1];\n+\treturn totals ? { authors: new Map(totals.a), bytes: totals.b } : NO_TOTALS;\n+}\n+\n+/** Each atom's author, read once. */\n+function authorsOf(graph: Graph): (id: AtomId) => string {\n+\tconst known = new Map<AtomId, string>();\n+\treturn (id) => {\n+\t\tlet author = known.get(id);\n+\t\tif (author === undefined) known.set(id, (author = graph.atom(id).author));\n+\t\treturn author;\n+\t};\n+}\n+\n+/** Totals in stored form: authors sorted, none at zero; null for a file with no lines. */\n+function normalTotals(authors: ReadonlyMap<string, number>, bytes: number): StoredTotals | null {\n+\tconst a = [...authors].filter(([, n]) => n !== 0).sort(([x], [y]) => (x < y ? -1 : x > y ? 1 : 0));\n+\treturn a.length === 0 && bytes === 0 ? null : { a, b: bytes };\n+}\n+\n+/**\n+ * A laid-out file's lines by author, from its visible lines' writers (abso Capacity): each writer's author once, instead\n+ * of making an atom id and a line id for every line (an agent's tree spent 0.4 s on that for its draft's files).\n+ */\n+export function authorsInView(\n+\tview: ReadView,\n+\tpath: string,\n+\tgraph: Graph,\n+\tauthorOf: (id: AtomId) => string,\n+): Map<string, number> {\n+\tconst out = new Map<string, number>();\n+\tconst writers = (view as { visibleWriters?(path: string): readonly number[] }).visibleWriters?.call(view, path);\n+\tif (!writers) {\n+\t\tfor (const l of view.lines(path)) {\n+\t\t\tconst author = authorOf(l.atom);\n+\t\t\tout.set(author, (out.get(author) ?? 0) + 1);\n+\t\t}\n+\t\treturn out;\n+\t}\n+\tconst bySeq = new Map<number, number>();\n+\tfor (const seq of writers) bySeq.set(seq, (bySeq.get(seq) ?? 0) + 1);\n+\tfor (const [seq, n] of bySeq) {\n+\t\tconst author = authorOf(graph.idOfSeq(seq));\n+\t\tout.set(author, (out.get(author) ?? 0) + n);\n+\t}\n+\treturn out;\n+}\n+\n+/** A file's totals from its layout in a view, as tree with authors counts them. */\n+function laidOutTotals(view: ReadView, path: string, authorOf: (id: AtomId) => string): StoredTotals | null {\n+\tif (!view.hasFile(path)) return null;\n+\tconst authors = authorsInView(view, path, view.graph, authorOf);\n+\treturn normalTotals(authors, authors.size === 0 ? 0 : utf8Bytes(view.text(path)));\n+}\n+\n+/** Keep a file's totals at version `v`, when they differ from what it had. */\n+function keepTotals(storage: GraphStateStorage, v: number, path: string, totals: StoredTotals | null): void {\n+\tconst was = (storage.read(AUTHORS, path) as StoredTotals | undefined) ?? null;\n+\tif (JSON.stringify(was) === JSON.stringify(totals)) return;\n+\tif (totals) storage.write(AUTHORS, path, totals);\n+\telse storage.remove(AUTHORS, path);\n+\tconst log = (storage.read(AUTHORS_LOG, path) as [number, StoredTotals | null][] | undefined) ?? [];\n+\tstorage.write(AUTHORS_LOG, path, [...log, [v, totals]]);\n+}\n+\n+/** The shared version the running counts describe, or undefined when this writer keeps none. */\n+export function countedVersion(storage: GraphStateStorage): number | undefined {\n+\treturn storage.read(META, \"version\") as number | undefined;\n+}\n+\n+export interface VersionFilesHost {\n+\treadonly storage: GraphStateStorage;\n+\treadonly graph: Graph;\n+\t/** Shared's set at a version (closed) and its view. */\n+\tat(version: number): ReadonlySet<AtomId>;\n+\tviewAt(version: number): ReadView;\n+\tempty(): ReadView;\n+\t/** Whether shared's closure changed at this version (spaces.ts). */\n+\tclosureChangedAt(version: number): boolean;\n+}\n+\n+/** A file's visible lines in a view: a laid-out count, for the slow path. */\n+function visibleCount(view: ReadView, path: string): number {\n+\tconst counted = (view as { visibleCount?(path: string): number }).visibleCount;\n+\treturn counted ? counted.call(view, path) : view.lines(path).length;\n+}\n+\n+/** The changed files of shared's version `v`, sorted, and which existed before and after; the running counts moved to `v`. */\n+export function versionFiles(\n+\thost: VersionFilesHost,\n+\tp: Published,\n+): { files: string[]; exists?: Map<string, readonly [boolean, boolean]> } {\n+\tconst { storage, graph } = host;\n+\tconst v = p.version;\n+\tconst counted = countedVersion(storage);\n+\tconst atoms = [...p.atoms, ...(p.dropped ?? [])];\n+\tconst before = v === 0 ? host.empty() : host.viewAt(v - 1);\n+\tconst after = host.viewAt(v);\n+\tconst fast =\n+\t\tv > 0 &&\n+\t\tcounted === v - 1 &&\n+\t\t!p.dropped?.length &&\n+\t\t!p.atoms.some((id) => graph.atom(id).moves?.length) &&\n+\t\t!host.closureChangedAt(v);\n+\t// The same moves as the version before: share them, and where lines sit, instead of resolving them again.\n+\tif (fast && before instanceof View && after instanceof View) after.shareMoves(before);\n+\tconst touched = touchedFiles(graph, atoms, [before, after]);\n+\t// Author totals start with the first version too, and are kept only while every version since has kept them.\n+\tconst authored = authoredVersion(storage);\n+\tconst authoring = authored === v - 1 || (v === 0 && authored === undefined);\n+\tconst authorOf = authorsOf(graph);\n+\tlet files: string[];\n+\tif (!fast) {\n+\t\tfiles = [...touched].filter((path) => differs(before, after, path)).sort();\n+\t\t// Counts start with the first version; a writer adopted before has none and keeps none.\n+\t\tif (counted === v - 1 || (v === 0 && counted === undefined)) {\n+\t\t\tfor (const path of files) storage.write(COUNTS, path, visibleCount(after, path));\n+\t\t\tstorage.write(META, \"version\", v);\n+\t\t}\n+\t\t// Every touched file's totals from its layout: one whose lines are the same texts by someone else isn't among\n+\t\t// the changed files, but its authors changed.\n+\t\tif (authoring) {\n+\t\t\tfor (const path of touched) keepTotals(storage, v, path, laidOutTotals(after, path, authorOf));\n+\t\t\tstorage.write(AUTHORS_META, \"version\", v);\n+\t\t}\n+\t\treturn { files };\n+\t}\n+\tconst beforeSet = host.at(v - 1);\n+\tconst afterSet = host.at(v);\n+\tconst visibleIn = (set: ReadonlySet<AtomId>, n: number) => {\n+\t\tfor (const seq of graph.eachDeleterSeqOf(n)) if (set.has(graph.idOfSeq(seq))) return false;\n+\t\treturn true;\n+\t};\n+\tconst fileOf = (view: ReadView, line: string, n: number) =>\n+\t\tview.hasMoves() ? view.fileOf(line) : graph.writtenIn(n);\n+\t// Per file: visible lines added minus removed, and the texts' balance; and, while totals are kept, each author's lines\n+\t// and the bytes added minus removed (a line and its newline, as joinLines makes the file).\n+\tconst changes = new Map<\n+\t\tstring,\n+\t\t{ net: number; texts: Map<string, number>; lines: number; authors: Map<string, number>; bytes: number }\n+\t>();\n+\tconst change = (path: string) => {\n+\t\tlet c = changes.get(path);\n+\t\tif (!c) changes.set(path, (c = { net: 0, texts: new Map(), lines: 0, authors: new Map(), bytes: 0 }));\n+\t\treturn c;\n+\t};\n+\tfor (const id of p.atoms) {\n+\t\tfor (const line of graph.eachInsertedLine(id)) {\n+\t\t\tconst n = graph.lineN(line);\n+\t\t\tif (!visibleIn(afterSet, n)) continue;\n+\t\t\tconst c = change(fileOf(after, line, n));\n+\t\t\tconst text = graph.textOf(n);\n+\t\t\tc.net++;\n+\t\t\tc.lines++;\n+\t\t\tc.texts.set(text, (c.texts.get(text) ?? 0) + 1);\n+\t\t\tif (authoring) {\n+\t\t\t\tconst author = authorOf(id);\n+\t\t\t\tc.authors.set(author, (c.authors.get(author) ?? 0) + 1);\n+\t\t\t\tc.bytes += utf8Bytes(text) + 1;\n+\t\t\t}\n+\t\t}\n+\t\tfor (const line of graph.deletedLines(id)) {\n+\t\t\tconst n = graph.lineN(line);\n+\t\t\tconst writer = graph.idOfSeq(graph.writerSeqOf(n));\n+\t\t\tif (!beforeSet.has(writer) || !visibleIn(beforeSet, n)) continue;\n+\t\t\tconst c = change(fileOf(before, line, n));\n+\t\t\tconst text = graph.textOf(n);\n+\t\t\tc.net--;\n+\t\t\tc.lines++;\n+\t\t\tc.texts.set(text, (c.texts.get(text) ?? 0) - 1);\n+\t\t\tif (authoring) {\n+\t\t\t\tconst author = authorOf(writer);\n+\t\t\t\tc.authors.set(author, (c.authors.get(author) ?? 0) - 1);\n+\t\t\t\tc.bytes -= utf8Bytes(text) + 1;\n+\t\t\t}\n+\t\t}\n+\t}\n+\tif (authoring) {\n+\t\tfor (const [path, c] of changes) {\n+\t\t\tif (c.lines === 0) continue;\n+\t\t\tconst was = storage.read(AUTHORS, path) as StoredTotals | undefined;\n+\t\t\tconst authors = new Map(was?.a ?? []);\n+\t\t\tfor (const [author, d] of c.authors) authors.set(author, (authors.get(author) ?? 0) + d);\n+\t\t\tkeepTotals(storage, v, path, normalTotals(authors, (was?.b ?? 0) + c.bytes));\n+\t\t}\n+\t\tstorage.write(AUTHORS_META, \"version\", v);\n+\t}\n+\tfiles = [];\n+\tconst exists = new Map<string, readonly [boolean, boolean]>();\n+\tfor (const path of touched) {\n+\t\tconst was = (storage.read(COUNTS, path) as number | undefined) ?? 0;\n+\t\tconst c = changes.get(path);\n+\t\tconst now = was + (c?.net ?? 0);\n+\t\tconst b = before.attrs(path);\n+\t\tconst a = after.attrs(path);\n+\t\tconst had = b.exists || b.link !== null || b.blob !== null || was > 0;\n+\t\tconst has = a.exists || a.link !== null || a.blob !== null || now > 0;\n+\t\tlet differ = had !== has || b.exec !== a.exec || b.link !== a.link || b.blob !== a.blob;\n+\t\tif (!differ && had && c && c.lines > 0) {\n+\t\t\t// Different texts in or out: the file's lines changed. The same texts in and out: compare the files.\n+\t\t\tdiffer = [...c.texts.values()].some((count) => count !== 0) || !before.sameText(path, after);\n+\t\t}\n+\t\tif (differ) {\n+\t\t\tfiles.push(path);\n+\t\t\texists.set(path, [had, has]);\n+\t\t}\n+\t\tif (now !== was) storage.write(COUNTS, path, now);\n+\t}\n+\tstorage.write(META, \"version\", v);\n+\treturn { files: files.sort(), exists };\n+}"}],"unmatched":[]}