{"draft":null,"from":91,"to":318,"files":[{"path":"worker/project/pathindex.ts","added":119,"removed":11,"diff":"--- a/worker/project/pathindex.ts\n+++ b/worker/project/pathindex.ts\n@@ -1,8 +1,9 @@\n+import { View } from \"../../src/engine/index.ts\";\n // Each version's files and shape, and each path's history in a space (#41; #46, disk-first 1). Built the first\n // time it's asked for and kept up as versions land; a version's files are also kept in the shapes table, so a\n // load needn't lay them out again.\n \n-import { type AtomId, type Graph, type LineId, View } from \"../../src/engine/index.ts\";\n+import type { AtomId, Graph, LineId, ReadView } from \"../../src/engine/index.ts\";\n import {\n \tdiffers,\n \ttype LastChange,\n@@ -17,11 +18,14 @@\n export interface PathIndexDeps {\n \treadonly graph: Graph;\n \tat(space: Space, version: number): ReadonlySet<AtomId>;\n-\tviewAt(space: Space, version: number): View;\n-\ttouchedFiles(ids: Iterable<AtomId>, views: readonly View[]): Set<string>;\n+\tviewAt(space: Space, version: number): ReadView;\n+\ttouchedFiles(ids: Iterable<AtomId>, views: readonly ReadView[]): Set<string>;\n \treadonly shapes: ProjectOptions[\"shapes\"];\n }\n \n+/** Older versions' file lists kept per space (a few agents' bases): about 1,000 paths each on abso. */\n+const OLDER_PATH_SETS = 8;\n+\n export class PathIndex {\n \treadonly #deps: PathIndexDeps;\n \n@@ -40,11 +44,91 @@\n \t/** Each version's files made and taken out, and moves (#41), worked out with its files. */\n \treadonly #shapeCache = new WeakMap<Space, Map<number, VersionShape>>();\n \n+\t/** The files at the newest version folded so far from kept shapes, per space (keptPaths). */\n+\treadonly #keptPaths = new WeakMap<Space, { version: number; files: Set<string> }>();\n+\t/** The files at a few older versions, worked back from the newest (agents' bases, mostly), most recent last. */\n+\treadonly #olderPaths = new WeakMap<Space, Map<number, Set<string>>>();\n+\n+\t/**\n+\t * The files at a version older than the newest folded (abso Capacity): the newest's files with each version since\n+\t * undone, its made files taken out and its gone ones put back. Folding from version 0 again for an agent's base\n+\t * read every version's kept row on each call.\n+\t */\n+\t#olderPathSet(\n+\t\tspace: Space,\n+\t\tfold: { version: number; files: Set<string> },\n+\t\tversion: number,\n+\t): ReadonlySet<string> | undefined {\n+\t\tlet older = this.#olderPaths.get(space);\n+\t\tif (!older) this.#olderPaths.set(space, (older = new Map()));\n+\t\tconst known = older.get(version);\n+\t\tif (known) {\n+\t\t\tolder.delete(version);\n+\t\t\tolder.set(version, known);\n+\t\t\treturn known;\n+\t\t}\n+\t\tconst files = new Set(fold.files);\n+\t\tfor (let v = fold.version; v > version; v--) {\n+\t\t\tif (!this.#keptFiles(space, v)) return undefined;\n+\t\t\tconst shape = this.#shapeCache.get(space)?.get(v);\n+\t\t\tif (!shape) return undefined;\n+\t\t\tfor (const f of shape.made) files.delete(f);\n+\t\t\tfor (const f of shape.gone) files.add(f);\n+\t\t}\n+\t\tolder.set(version, files);\n+\t\tif (older.size > OLDER_PATH_SETS) older.delete(older.keys().next().value!);\n+\t\treturn files;\n+\t}\n+\n+\t/**\n+\t * The files a space has at a version, from the kept rows of every version up to it: each one's files made and\n+\t * taken out, folded from version 0 (abso Capacity). Undefined when a version has no kept row that describes it;\n+\t * then the caller lays the version out.\n+\t */\n+\tkeptPaths(space: Space, version: number): string[] | undefined {\n+\t\tconst files = this.keptPathSet(space, version);\n+\t\treturn files && [...files].sort();\n+\t}\n+\n+\t/**\n+\t * The files the versions after `from`, up to `to`, changed, from each version's kept row (abso 738): undefined when one\n+\t * has none, and the caller works them out from the atoms. Every file whose text differs between the two versions is\n+\t * in it, since some version between changed it.\n+\t */\n+\tkeptChangesSince(space: Space, from: number, to: number): ReadonlySet<string> | undefined {\n+\t\tconst out = new Set<string>();\n+\t\tfor (let v = from + 1; v <= to; v++) {\n+\t\t\tconst files = this.#keptFiles(space, v);\n+\t\t\tif (!files) return undefined;\n+\t\t\tfor (const f of files) out.add(f);\n+\t\t}\n+\t\treturn out;\n+\t}\n+\n+\t/** keptPaths as a set, kept for the newest version asked: never changed once made, so a view can hold it. */\n+\tkeptPathSet(space: Space, version: number): ReadonlySet<string> | undefined {\n+\t\tlet fold = this.#keptPaths.get(space);\n+\t\tif (fold?.version === version) return fold.files;\n+\t\tif (fold && fold.version > version) return this.#olderPathSet(space, fold, version);\n+\t\tif (!fold) fold = { version: -1, files: new Set() };\n+\t\tconst files = new Set(fold.files);\n+\t\tfor (let v = fold.version + 1; v <= version; v++) {\n+\t\t\tif (!this.#keptFiles(space, v)) return undefined;\n+\t\t\t// Not yet when the version is still being kept (keep asks its views which files exist).\n+\t\t\tconst shape = this.#shapeCache.get(space)?.get(v);\n+\t\t\tif (!shape) return undefined;\n+\t\t\tfor (const f of shape.gone) files.delete(f);\n+\t\t\tfor (const f of shape.made) files.add(f);\n+\t\t}\n+\t\tthis.#keptPaths.set(space, { version, files });\n+\t\treturn files;\n+\t}\n+\n \t/** Each space's path index (#41): built the first time it's asked for, then kept up as versions land. */\n \treadonly #pathIndex = new WeakMap<Space, { upTo: number; paths: Map<string, PathLife> }>();\n \n \t/** The files a version changed. `views` holds views by version, so a run of versions builds each once. */\n-\tversionFiles(space: Space, version: number, views = new Map<number, View>()): string[] {\n+\tversionFiles(space: Space, version: number, views = new Map<number, ReadView>()): string[] {\n \t\tlet cache = this.#filesCache.get(space);\n \t\tif (!cache) this.#filesCache.set(space, (cache = new Map()));\n \t\tconst known = cache.get(version);\n@@ -53,7 +137,7 @@\n \t\tif (kept) return kept;\n \t\tconst shapes = this.#shapeCache.get(space)!;\n \t\tconst p = space.versions[version]!;\n-\t\tconst viewAt = (v: number): View => {\n+\t\tconst viewAt = (v: number): ReadView => {\n \t\t\tlet view = views.get(v);\n \t\t\tif (!view) views.set(v, (view = v < 0 ? new View(this.#deps.graph, []) : this.#deps.viewAt(space, v)));\n \t\t\treturn view;\n@@ -63,11 +147,34 @@\n \t\t// Net changes only, as show has them: a file made and removed again before the publish isn't one (#37).\n \t\tconst touched = this.#deps.touchedFiles([...p.atoms, ...(p.dropped ?? [])], [before, after]);\n \t\tconst files = [...touched].filter((path) => differs(before, after, path)).sort();\n+\t\tthis.keep(space, version, files, before, after);\n+\t\treturn files;\n+\t}\n+\n+\t/**\n+\t * Keep a version's changed files, with its files made and taken out and its moves: worked out here, or as the\n+\t * version landed (version-files.ts, abso Capacity), which says which files existed before and after.\n+\t */\n+\tkeep(\n+\t\tspace: Space,\n+\t\tversion: number,\n+\t\tfiles: string[],\n+\t\tbefore: ReadView,\n+\t\tafter: ReadView,\n+\t\texists?: ReadonlyMap<string, readonly [boolean, boolean]>,\n+\t): void {\n+\t\tlet cache = this.#filesCache.get(space);\n+\t\tif (!cache) this.#filesCache.set(space, (cache = new Map()));\n+\t\tlet shapes = this.#shapeCache.get(space);\n+\t\tif (!shapes) this.#shapeCache.set(space, (shapes = new Map()));\n+\t\tconst p = space.versions[version]!;\n \t\tcache.set(version, files);\n \t\t// Files made and taken out, and which of those are one file moved (#41): an atom that moves lines\n \t\t// names both files, and a binary or empty file moved keeps its blob.\n-\t\tconst made = files.filter((f) => !before.hasFile(f) && after.hasFile(f));\n-\t\tconst gone = files.filter((f) => before.hasFile(f) && !after.hasFile(f));\n+\t\tconst had = (f: string) => exists?.get(f)?.[0] ?? before.hasFile(f);\n+\t\tconst has = (f: string) => exists?.get(f)?.[1] ?? after.hasFile(f);\n+\t\tconst made = files.filter((f) => !had(f) && has(f));\n+\t\tconst gone = files.filter((f) => had(f) && !has(f));\n \t\tconst moves: [string, string][] = [];\n \t\tif (made.length > 0 && gone.length > 0) {\n \t\t\tconst movers = new Set(this.#deps.graph.movers());\n@@ -86,7 +193,6 @@\n \t\t}\n \t\tshapes.set(version, { made, gone, moves });\n \t\tthis.#deps.shapes?.put(space.name, version, JSON.stringify({ stamp: shapeStamp(p), files, made, gone, moves }));\n-\t\treturn files;\n \t}\n \n \t/** A version's files kept from an earlier load (#41), when the row describes this very version; into the caches. */\n@@ -107,7 +213,7 @@\n \t}\n \n \t/** A version's files made and taken out, and moves (#41). */\n-\tversionShape(space: Space, version: number, views?: Map<number, View>): VersionShape {\n+\tversionShape(space: Space, version: number, views?: Map<number, ReadView>): VersionShape {\n \t\tconst known = this.#shapeCache.get(space)?.get(version);\n \t\tif (known) return known;\n \t\tthis.#filesCache.get(space)?.delete(version);\n@@ -124,7 +230,7 @@\n \tpathsOf(space: Space): Map<string, PathLife> {\n \t\tlet index = this.#pathIndex.get(space);\n \t\tif (!index) this.#pathIndex.set(space, (index = { upTo: -1, paths: new Map() }));\n-\t\tconst views = new Map<number, View>();\n+\t\tconst views = new Map<number, ReadView>();\n \t\t// Shared's sets grow version by version, so walk them forward rather than build each from the\n \t\t// start; the views stay out of the cache, which they would only push other views from. A version\n \t\t// whose files are kept (#41) needs no views at all.\n@@ -140,7 +246,9 @@\n \t\t\treturn running;\n \t\t};\n \t\tfor (let v = index.upTo + 1; v <= last(space); v++) {\n-\t\t\tif (space.name === SHARED && !this.#keptFiles(space, v)) {\n+\t\t\t// Persistent graphs use the indexed version reader; a raw View would\n+\t\t\t// rebuild move scratch state and copy historical membership during a read.\n+\t\t\tif (space.name === SHARED && !this.#deps.graph.hasOrderedChildren && !this.#keptFiles(space, v)) {\n \t\t\t\tif (v > 0 && !views.has(v - 1)) views.set(v - 1, new View(this.#deps.graph, new Set(setAt(v - 1))));\n \t\t\t\tviews.set(v, new View(this.#deps.graph, new Set(setAt(v))));\n \t\t\t}"}],"unmatched":[]}