{"draft":null,"from":175,"to":318,"files":[{"path":"worker/project/pathindex.ts","added":108,"removed":3,"diff":"--- a/worker/project/pathindex.ts\n+++ b/worker/project/pathindex.ts\n@@ -23,6 +23,9 @@\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@@ -41,6 +44,86 @@\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@@ -64,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@@ -87,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. */"}],"unmatched":[]}