- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/spaces.ts?draft=login-timeout&format=json
188 linesFirst import 100%0 versions changed it
1// Version membership (#46, disk-first 1): which atoms are in which space at which version. Shared ('') and
2// named drafts, their versions, what each version added and dropped, and the shared version a draft builds
3// on. Project and the derived tables (tables/, issue 47) both keep it from the same entries, so the rules
4// live here once.
6import type { AtomId } from "../../src/engine/index.ts";
7import type { Entry } from "./project.ts";
9export const SHARED = "";
11export interface Published {
12 readonly version: number;
13 readonly agent: string;
14 /** The message now (an amend can correct it). */
15 message: string;
16 /** The message it was published with, once amended. */
17 originalMessage?: string;
18 readonly time: number;
19 /** Everything this version added to its space (changes and tie). */
20 readonly atoms: readonly AtomId[];
21 /** For a named draft: the version of shared this version builds on. */
22 readonly sharedBase?: number;
23 /** For a drop: the atoms it took out, and the versions they came from. */
24 readonly dropped?: readonly AtomId[];
25 readonly droppedVersions?: readonly number[];
26}
28export /** Shared ('') or a named draft. */
29interface Space {
30 readonly name: string;
31 readonly versions: Published[];
32 /** Every atom published into this space (for shared, everything in it). */
33 readonly own: Set<AtomId>;
34 readonly publishedIn: Map<AtomId, number>;
35 /** Atoms a drop took out, and the version of the drop. */
36 readonly droppedIn: Map<AtomId, number>;
37 readonly atCache: Map<number, ReadonlySet<AtomId>>;
38 /** For a named draft, the log position of the entry that made it: the change its work belongs to (#44). */
39 startedAt?: number;
40}
42export function newSpace(name: string): Space {
43 return { name, versions: [], own: new Set(), publishedIn: new Map(), droppedIn: new Map(), atCache: new Map() };
44}
46export function addVersion(space: Space, version: Published): void {
47 space.versions.push(version);
48 for (const id of version.atoms) {
49 space.own.add(id);
50 space.publishedIn.set(id, version.version);
51 }
52 for (const id of version.dropped ?? []) {
53 space.own.delete(id);
54 space.droppedIn.set(id, version.version);
55 }
56}
58export function last(space: Space): number {
59 return space.versions.length - 1;
60}
62export function sharedBaseOf(space: Space): number {
63 return space.versions[space.versions.length - 1]!.sharedBase ?? 0;
64}
66/** Every space, and each one's contents at any of its versions. */
67export class Membership {
68 readonly spaces = new Map<string, Space>([[SHARED, newSpace(SHARED)]]);
70 get shared(): Space {
71 return this.spaces.get(SHARED)!;
72 }
74 /** What an entry (at log position `at`) does to membership; other kinds change none. */
75 apply(entry: Entry, at: number): void {
76 switch (entry.type) {
77 case "init":
78 addVersion(this.shared, {
79 version: 0,
80 agent: "init",
81 message: entry.atom.message,
82 time: entry.time,
83 atoms: [entry.atom.id],
84 });
85 break;
86 case "publish": {
87 const space = this.spaces.get(entry.space ?? SHARED)!;
88 const atoms = [...entry.changes, entry.tie.id];
89 addVersion(space, {
90 version: entry.version,
91 agent: entry.agent,
92 message: entry.message,
93 time: entry.time,
94 atoms,
95 ...(space.name !== SHARED ? { sharedBase: sharedBaseOf(space) } : {}),
96 });
97 break;
98 }
99 case "drop":
100 addVersion(this.shared, {
101 version: entry.version,
102 agent: entry.agent,
103 message: entry.message,
104 time: entry.time,
105 atoms: [],
106 dropped: entry.atoms,
107 droppedVersions: entry.versions,
108 });
109 break;
110 case "amend": {
111 const p = this.spaces.get(entry.space ?? SHARED)!.versions[entry.version]!;
112 p.originalMessage ??= p.message;
113 p.message = entry.message;
114 break;
115 }
116 case "space": {
117 const space = newSpace(entry.name);
118 space.startedAt = at;
119 this.spaces.set(entry.name, space);
120 addVersion(space, {
121 version: 0,
122 agent: entry.agent,
123 message: `Start draft ${entry.name}`,
124 time: entry.time,
125 atoms: [],
126 sharedBase: entry.sharedBase,
127 });
128 break;
129 }
130 case "space-sync": {
131 const space = this.spaces.get(entry.space)!;
132 addVersion(space, {
133 version: entry.version,
134 agent: entry.agent,
135 message: `Move draft ${entry.space} onto shared version ${entry.sharedBase}`,
136 time: entry.time,
137 atoms: [],
138 sharedBase: entry.sharedBase,
139 });
140 break;
141 }
142 case "land": {
143 const atoms = [...entry.changes, entry.tie.id];
144 addVersion(this.shared, {
145 version: entry.version,
146 agent: entry.agent,
147 message: entry.message,
148 time: entry.time,
149 atoms,
150 });
151 if (entry.rebase) {
152 const space = this.spaces.get(entry.space)!;
153 addVersion(space, {
154 version: last(space) + 1,
155 agent: entry.agent,
156 message: `Landed in shared version ${entry.version}`,
157 time: entry.time,
158 atoms: [],
159 sharedBase: entry.version,
160 });
161 }
162 break;
163 }
164 }
165 }
167 /**
168 * A space's contents at one of its versions. For shared's latest version this is `shared.own` itself, not a
169 * copy (it is read on every publish and sync, and copying it would cost a set per call): it changes when the
170 * next version is applied. A caller that keeps it across an apply copies it first.
171 */
172 at(space: Space, version: number): ReadonlySet<AtomId> {
173 if (space.name === SHARED && version === last(space)) return space.own;
174 let set = space.atCache.get(version);
175 if (!set) {
176 const s = new Set<AtomId>();
177 if (space.name !== SHARED) for (const id of this.at(this.shared, space.versions[version]!.sharedBase!)) s.add(id);
178 for (let v = 0; v <= version; v++) {
179 for (const id of space.versions[v]!.atoms) s.add(id);
180 for (const id of space.versions[v]!.dropped ?? []) s.delete(id);
181 }
182 set = s;
183 if (space.atCache.size > 16) space.atCache.clear();
184 space.atCache.set(version, set);
185 }
186 return set;
187 }
188}