{"draft":null,"from":2,"to":318,"files":[{"path":"worker/project/without.ts","added":183,"removed":0,"diff":"--- /dev/null\n+++ b/worker/project/without.ts\n@@ -0,0 +1,183 @@\n+// A version of shared without some earlier versions: the files that version's atoms make when the atoms those\n+// versions added are left out. Shared doesn't change, and nothing is written: it is a read. The atoms keep\n+// their ids and authors; nothing is replayed or renumbered.\n+//\n+// The set left is checked exactly as asked. When a kept version builds on a left-out one, the answer is a\n+// refusal naming that chain, the files and the wider set that would work. It is never widened silently, and\n+// never closed again by a helper that would bring a left-out atom back.\n+\n+import { type AtomId, type Graph, type ReadView, View } from \"../../src/engine/index.ts\";\n+import { ProjectError } from \"./project-error.ts\";\n+import type { FileChange } from \"./project-types.ts\";\n+import type { Space } from \"./spaces.ts\";\n+import { treeHash } from \"./tree.ts\";\n+import { changesBetween, modesOf } from \"./view-changes.ts\";\n+\n+/** What a version without others needs from a project. */\n+export interface WithoutHost {\n+\treadonly graph: Graph;\n+\tshared(): Space;\n+\tneedInit(): void;\n+\tcheckVersion(space: Space, version: number): void;\n+\tviewAt(space: Space, version: number): ReadView;\n+\tat(space: Space, version: number): ReadonlySet<AtomId>;\n+\t/** The set as a view of it reads it: with any discarded atom a publish still depends on. */\n+\tclosed(set: ReadonlySet<AtomId>): ReadonlySet<AtomId>;\n+\ttouchedFiles(ids: Iterable<AtomId>, views: readonly ReadView[]): Set<string>;\n+}\n+\n+/** A version, as the answer names it. */\n+export interface VersionNamed {\n+\tversion: number;\n+\tagent: string;\n+\tmessage: string;\n+}\n+\n+/** A kept version that builds on a left-out one: the versions it builds on, and in which files. */\n+export interface BuildsOn extends VersionNamed {\n+\tneeds: { version: number; files: string[] }[];\n+}\n+\n+export type WithoutAnswer =\n+\t| {\n+\t\t\tok: true;\n+\t\t\tversion: number;\n+\t\t\twithout: VersionNamed[];\n+\t\t\t/** Every file that differs from the version as it is: before, in it; after, without the others. */\n+\t\t\tfiles: FileChange[];\n+\t\t\t/** The tree hash (tree.ts) of every file without them: the same ask always gives the same hash. */\n+\t\t\ttree: string;\n+\t  }\n+\t| {\n+\t\t\tok: false;\n+\t\t\tversion: number;\n+\t\t\twithout: VersionNamed[];\n+\t\t\t/** Kept versions that build on left-out ones, oldest first: each with what it builds on. */\n+\t\t\tdependents: BuildsOn[];\n+\t\t\t/** The versions to leave out for it to work: these and every dependent. A suggestion, never applied. */\n+\t\t\tsuggestion: number[];\n+\t  };\n+\n+/**\n+ * Version `version` of shared without the versions in `without`: what its\n+ * files are when those versions' atoms are left out. Refused, with the\n+ * chain, when a version kept builds on one left out.\n+ */\n+export function without(host: WithoutHost, version: number, versions: readonly number[]): WithoutAnswer {\n+\thost.needInit();\n+\tconst shared = host.shared();\n+\tif (!Number.isInteger(version)) throw new ProjectError(\"bad-request\", \"version must be a number\");\n+\thost.checkVersion(shared, version);\n+\tif (!Array.isArray(versions) || versions.length === 0)\n+\t\tthrow new ProjectError(\"bad-request\", \"without needs the versions to leave out\");\n+\tconst g = host.graph;\n+\tconst asked = [...new Set(versions)].toSorted((a, b) => a - b);\n+\tconst base = host.at(shared, version);\n+\tconst out = new Set<AtomId>();\n+\tfor (const v of asked) {\n+\t\tif (!Number.isInteger(v) || v < 1)\n+\t\t\tthrow new ProjectError(\n+\t\t\t\t\"bad-request\",\n+\t\t\t\t`there is no version ${String(v)} to leave out (version 0 is the start, which everything builds on)`,\n+\t\t\t);\n+\t\tif (v > version)\n+\t\t\tthrow new ProjectError(\"bad-request\", `version ${v} comes after version ${version}, so it isn't in it`);\n+\t\tconst p = shared.versions[v]!;\n+\t\tif (p.atoms.length === 0)\n+\t\t\tthrow new ProjectError(\n+\t\t\t\t\"bad-request\",\n+\t\t\t\t`version ${v} added nothing (it is a drop), so there is nothing to leave out`,\n+\t\t\t);\n+\t\tconst left = p.atoms.filter((id) => base.has(id));\n+\t\tif (left.length === 0)\n+\t\t\tthrow new ProjectError(\n+\t\t\t\t\"bad-request\",\n+\t\t\t\t`version ${v} isn't in version ${version}: version ${shared.droppedIn.get(p.atoms[0]!)} dropped it`,\n+\t\t\t);\n+\t\tfor (const id of left) out.add(id);\n+\t}\n+\tconst describe = (v: number): VersionNamed => ({\n+\t\tversion: v,\n+\t\tagent: shared.versions[v]!.agent,\n+\t\tmessage: shared.versions[v]!.message,\n+\t});\n+\tconst kept = new Set<AtomId>();\n+\tfor (const id of host.closed(base)) if (!out.has(id)) kept.add(id);\n+\n+\t// Everything kept that builds on what is left out, and the step that reached it.\n+\tconst gone = new Set(out);\n+\tconst steps: { from: AtomId; to: AtomId }[] = [];\n+\tconst stack = [...out];\n+\twhile (stack.length > 0) {\n+\t\tconst id = stack.pop()!;\n+\t\tfor (const d of g.dependentsOf(id)) {\n+\t\t\tif (!kept.has(d)) continue;\n+\t\t\tsteps.push({ from: d, to: id });\n+\t\t\tif (gone.has(d)) continue;\n+\t\t\tgone.add(d);\n+\t\t\tstack.push(d);\n+\t\t}\n+\t}\n+\tif (gone.size > out.size) {\n+\t\t// A discarded atom a publish still needs has no version of its own: it counts with the version that needs it.\n+\t\tconst versionOf = (id: AtomId): number | undefined => shared.publishedIn.get(id);\n+\t\tconst needs = new Map<number, Map<number, Set<string>>>();\n+\t\tconst resolved = (id: AtomId, seen = new Set<AtomId>()): number[] => {\n+\t\t\tconst v = versionOf(id);\n+\t\t\tif (v !== undefined) return [v];\n+\t\t\tif (seen.has(id)) return [];\n+\t\t\tseen.add(id);\n+\t\t\treturn [...new Set(steps.filter((s) => s.from === id).flatMap((s) => resolved(s.to, seen)))];\n+\t\t};\n+\t\tfor (const { from, to } of steps) {\n+\t\t\tconst dv = versionOf(from);\n+\t\t\tif (dv === undefined) continue;\n+\t\t\tconst files = g.filesOf(from).length > 0 ? g.filesOf(from) : g.filesOf(to);\n+\t\t\tfor (const nv of resolved(to)) {\n+\t\t\t\tif (nv === dv) continue;\n+\t\t\t\tlet of = needs.get(dv);\n+\t\t\t\tif (!of) needs.set(dv, (of = new Map()));\n+\t\t\t\tlet set = of.get(nv);\n+\t\t\t\tif (!set) of.set(nv, (set = new Set()));\n+\t\t\t\tfor (const f of files) set.add(f);\n+\t\t\t}\n+\t\t}\n+\t\tconst dependents = [...needs.keys()]\n+\t\t\t.filter((v) => !asked.includes(v))\n+\t\t\t.toSorted((a, b) => a - b)\n+\t\t\t.map((v) => ({\n+\t\t\t\t...describe(v),\n+\t\t\t\tneeds: [...needs.get(v)!]\n+\t\t\t\t\t.toSorted(([a], [b]) => a - b)\n+\t\t\t\t\t.map(([n, files]) => ({ version: n, files: [...files].toSorted() })),\n+\t\t\t}));\n+\t\treturn {\n+\t\t\tok: false,\n+\t\t\tversion,\n+\t\t\twithout: asked.map(describe),\n+\t\t\tdependents,\n+\t\t\tsuggestion: [...new Set([...asked, ...dependents.map((d) => d.version)])].toSorted((a, b) => a - b),\n+\t\t};\n+\t}\n+\n+\t// The set is checked; the engine's View refuses one that isn't closed, so nothing left out comes back.\n+\tconst before = host.viewAt(shared, version);\n+\tconst after = new View(g, kept);\n+\tconst files = changesBetween(before, after, host.touchedFiles(out, [before, after]));\n+\tconst all = after.paths();\n+\tconst blobs: Record<string, string> = {};\n+\tconst texts: Record<string, string> = {};\n+\tfor (const path of all) {\n+\t\tconst blob = after.attrs(path).blob;\n+\t\tif (blob !== null) blobs[path] = blob;\n+\t\telse texts[path] = after.text(path);\n+\t}\n+\tconst modes = modesOf(after);\n+\treturn {\n+\t\tok: true,\n+\t\tversion,\n+\t\twithout: asked.map(describe),\n+\t\tfiles,\n+\t\ttree: treeHash({ files: texts, blobs, modes }),\n+\t};\n+}"}],"unmatched":[]}