Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed
MCP
code on this site's /mcp
CLI
none: the JSON below is the answer
JSON
/christopher/abso-core/files/worker/project/without.ts?format=json
183 linescandidates 100%Last change candidates in version 186, 1 version changed it
1// A version of shared without some earlier versions: the files that version's atoms make when the atoms those
2// versions added are left out. Shared doesn't change, and nothing is written: it is a read. The atoms keep
3// their ids and authors; nothing is replayed or renumbered.
4//
5// The set left is checked exactly as asked. When a kept version builds on a left-out one, the answer is a
6// refusal naming that chain, the files and the wider set that would work. It is never widened silently, and
7// never closed again by a helper that would bring a left-out atom back.
8
9import { type AtomId, type Graph, type ReadView, View } from "../../src/engine/index.ts";
10import { ProjectError } from "./project-error.ts";
11import type { FileChange } from "./project-types.ts";
12import type { Space } from "./spaces.ts";
13import { treeHash } from "./tree.ts";
14import { changesBetween, modesOf } from "./view-changes.ts";
16/** What a version without others needs from a project. */
17export interface WithoutHost {
18 readonly graph: Graph;
19 shared(): Space;
20 needInit(): void;
21 checkVersion(space: Space, version: number): void;
22 viewAt(space: Space, version: number): ReadView;
23 at(space: Space, version: number): ReadonlySet<AtomId>;
24 /** The set as a view of it reads it: with any discarded atom a publish still depends on. */
25 closed(set: ReadonlySet<AtomId>): ReadonlySet<AtomId>;
26 touchedFiles(ids: Iterable<AtomId>, views: readonly ReadView[]): Set<string>;
29/** A version, as the answer names it. */
30export interface VersionNamed {
31 version: number;
32 agent: string;
33 message: string;
36/** A kept version that builds on a left-out one: the versions it builds on, and in which files. */
37export interface BuildsOn extends VersionNamed {
38 needs: { version: number; files: string[] }[];
41export type WithoutAnswer =
42 | {
43 ok: true;
44 version: number;
45 without: VersionNamed[];
46 /** Every file that differs from the version as it is: before, in it; after, without the others. */
47 files: FileChange[];
48 /** The tree hash (tree.ts) of every file without them: the same ask always gives the same hash. */
49 tree: string;
50 }
51 | {
52 ok: false;
53 version: number;
54 without: VersionNamed[];
55 /** Kept versions that build on left-out ones, oldest first: each with what it builds on. */
56 dependents: BuildsOn[];
57 /** The versions to leave out for it to work: these and every dependent. A suggestion, never applied. */
58 suggestion: number[];
59 };
61/**
62 * Version `version` of shared without the versions in `without`: what its
63 * files are when those versions' atoms are left out. Refused, with the
64 * chain, when a version kept builds on one left out.
65 */
66export function without(host: WithoutHost, version: number, versions: readonly number[]): WithoutAnswer {
67 host.needInit();
68 const shared = host.shared();
69 if (!Number.isInteger(version)) throw new ProjectError("bad-request", "version must be a number");
70 host.checkVersion(shared, version);
71 if (!Array.isArray(versions) || versions.length === 0)
72 throw new ProjectError("bad-request", "without needs the versions to leave out");
73 const g = host.graph;
74 const asked = [...new Set(versions)].toSorted((a, b) => a - b);
75 const base = host.at(shared, version);
76 const out = new Set<AtomId>();
77 for (const v of asked) {
78 if (!Number.isInteger(v) || v < 1)
79 throw new ProjectError(
80 "bad-request",
81 `there is no version ${String(v)} to leave out (version 0 is the start, which everything builds on)`,
82 );
83 if (v > version)
84 throw new ProjectError("bad-request", `version ${v} comes after version ${version}, so it isn't in it`);
85 const p = shared.versions[v]!;
86 if (p.atoms.length === 0)
87 throw new ProjectError(
88 "bad-request",
89 `version ${v} added nothing (it is a drop), so there is nothing to leave out`,
90 );
91 const left = p.atoms.filter((id) => base.has(id));
92 if (left.length === 0)
93 throw new ProjectError(
94 "bad-request",
95 `version ${v} isn't in version ${version}: version ${shared.droppedIn.get(p.atoms[0]!)} dropped it`,
96 );
97 for (const id of left) out.add(id);
98 }
99 const describe = (v: number): VersionNamed => ({
100 version: v,
101 agent: shared.versions[v]!.agent,
102 message: shared.versions[v]!.message,
103 });
104 const kept = new Set<AtomId>();
105 for (const id of host.closed(base)) if (!out.has(id)) kept.add(id);
107 // Everything kept that builds on what is left out, and the step that reached it.
108 const gone = new Set(out);
109 const steps: { from: AtomId; to: AtomId }[] = [];
110 const stack = [...out];
111 while (stack.length > 0) {
112 const id = stack.pop()!;
113 for (const d of g.dependentsOf(id)) {
114 if (!kept.has(d)) continue;
115 steps.push({ from: d, to: id });
116 if (gone.has(d)) continue;
117 gone.add(d);
118 stack.push(d);
119 }
120 }
121 if (gone.size > out.size) {
122 // A discarded atom a publish still needs has no version of its own: it counts with the version that needs it.
123 const versionOf = (id: AtomId): number | undefined => shared.publishedIn.get(id);
124 const needs = new Map<number, Map<number, Set<string>>>();
125 const resolved = (id: AtomId, seen = new Set<AtomId>()): number[] => {
126 const v = versionOf(id);
127 if (v !== undefined) return [v];
128 if (seen.has(id)) return [];
129 seen.add(id);
130 return [...new Set(steps.filter((s) => s.from === id).flatMap((s) => resolved(s.to, seen)))];
131 };
132 for (const { from, to } of steps) {
133 const dv = versionOf(from);
134 if (dv === undefined) continue;
135 const files = g.filesOf(from).length > 0 ? g.filesOf(from) : g.filesOf(to);
136 for (const nv of resolved(to)) {
137 if (nv === dv) continue;
138 let of = needs.get(dv);
139 if (!of) needs.set(dv, (of = new Map()));
140 let set = of.get(nv);
141 if (!set) of.set(nv, (set = new Set()));
142 for (const f of files) set.add(f);
143 }
144 }
145 const dependents = [...needs.keys()]
146 .filter((v) => !asked.includes(v))
147 .toSorted((a, b) => a - b)
148 .map((v) => ({
149 ...describe(v),
150 needs: [...needs.get(v)!]
151 .toSorted(([a], [b]) => a - b)
152 .map(([n, files]) => ({ version: n, files: [...files].toSorted() })),
153 }));
154 return {
155 ok: false,
156 version,
157 without: asked.map(describe),
158 dependents,
159 suggestion: [...new Set([...asked, ...dependents.map((d) => d.version)])].toSorted((a, b) => a - b),
160 };
161 }
163 // The set is checked; the engine's View refuses one that isn't closed, so nothing left out comes back.
164 const before = host.viewAt(shared, version);
165 const after = new View(g, kept);
166 const files = changesBetween(before, after, host.touchedFiles(out, [before, after]));
167 const all = after.paths();
168 const blobs: Record<string, string> = {};
169 const texts: Record<string, string> = {};
170 for (const path of all) {
171 const blob = after.attrs(path).blob;
172 if (blob !== null) blobs[path] = blob;
173 else texts[path] = after.text(path);
174 }
175 const modes = modesOf(after);
176 return {
177 ok: true,
178 version,
179 without: asked.map(describe),
180 files,
181 tree: treeHash({ files: texts, blobs, modes }),
182 };