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
223 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 166, 4 versions changed it

You're reading version 170. The newest is 318, and 1 version since then changed this file. Go to the newestCompare with 318

1import { startNode } from "../keys.ts";
2import { StableSet } from "../state-source.ts";
3import type { FileAttrs, ReadView, SeqLine, VisibleLine } from "../view.ts";
4import type { ExperimentalMarkerProjection, MarkerState } from "./projection-marker.ts";
5
6/** Private reader for a host-verified immutable projection. Not a production View replacement. */
7export class ProjectionReader implements ReadView {
8 readonly #adapter: ExperimentalMarkerProjection;
9 readonly #state: MarkerState;
10 readonly #assertLive: () => void;
11 constructor(adapter: ExperimentalMarkerProjection, state: MarkerState, assertLive: () => void) {
12 assertLive();
13 this.#assertLive = assertLive;
14 adapter.assertContext(state);
15 this.#adapter = adapter;
16 this.#state = structuredClone(state);
17 }
18 get graph() {
19 this.#assertLive();
20 return this.#adapter.graph;
21 }
22 get set(): ReadonlySet<string> {
23 this.#assertLive();
24 const has = (id: string) =>
25 this.graph.has(id) && (this.#adapter.members.get(this.#state.membership, Number(id.slice(1)))?.present ?? false);
26 const assertLive = this.#assertLive;
27 const members = this.#adapter.members;
28 const root = this.#state.membership;
29 const graph = this.graph;
30 return new (class extends StableSet<string> {
31 readonly retainedBytes = 64;
32 has(id: string): boolean {
33 assertLive();
34 return has(id);
35 }
36 *values(): SetIterator<string> {
37 assertLive();
38 const iterator = members.entries(root);
39 try {
40 for (;;) {
41 assertLive();
42 const item = iterator.next();
43 if (item.done) return;
44 const [seq, member] = item.value;
45 if (member.present) yield graph.idOfSeq(seq);
46 }
47 } finally {
48 iterator.return?.();
49 }
50 }
51 })();
52 }
53 paths(): string[] {
54 this.#assertLive();
55 return this.knownPaths().filter((path) => this.hasFile(path));
56 }
57 files(): Record<string, string> {
58 this.#assertLive();
59 return Object.fromEntries(
60 this.paths()
61 .filter((path) => this.attrs(path).blob === null)
62 .map((path) => [path, this.text(path)]),
63 );
64 }
65 blobs(): Record<string, string> {
66 this.#assertLive();
67 const result: Record<string, string> = {};
68 for (const path of this.paths()) {
69 const blob = this.attrs(path).blob;
70 if (blob !== null) result[path] = blob;
71 }
72 return result;
73 }
74 lineNumber(path: string, line: string): number {
75 this.#assertLive();
76 let index = 0;
77 for (const id of this.#adapter.lineIds(this.#state, path)) {
78 this.#assertLive();
79 if (id === line) return index;
80 index++;
81 }
82 this.#assertLive();
83 return -1;
84 }
85 sameText(path: string, other: ReadView): boolean {
86 this.#assertLive();
87 const a = this.lines(path),
88 b = other.lines(path);
89 return a.length === b.length && a.every((line, index) => line.text === b[index]!.text);
90 }
91 visibleNeighbours(_path: string, _line: string): string[] {
92 this.#assertLive();
93 throw Error("unsupported projected visibleNeighbours");
94 }
95 weight(): number {
96 this.#assertLive();
97 throw Error("transaction projections cannot enter historical View cache");
98 }
100 attrs(path: string): FileAttrs {
101 this.#assertLive();
102 return this.#adapter.attrs(this.#state, path);
103 }
104 knownPaths(): string[] {
105 this.#assertLive();
106 return this.#adapter.knownPaths(this.#state);
107 }
108 hasMoves(): boolean {
109 this.#assertLive();
110 for (const id of this.graph.movers()) if (this.set.has(id)) return true;
111 return false;
112 }
113 *visibleLines(path: string): IterableIterator<VisibleLine> {
114 this.#assertLive();
115 const graph = this.#adapter.graph;
116 const iterator = this.#adapter.lineIds(this.#state, path);
117 while (true) {
118 this.#assertLive();
119 const next = iterator.next();
120 this.#assertLive();
121 if (next.done) return;
122 const id = next.value;
123 const n = graph.lineN(id);
124 yield { id, text: graph.textOf(n), atom: graph.idOfSeq(graph.writerSeqOf(n)) };
125 }
126 }
127 lines(path: string): readonly VisibleLine[] {
128 this.#assertLive();
129 return [...this.visibleLines(path)];
130 }
131 sequence(path: string): readonly SeqLine[] {
132 this.#assertLive();
133 const adapter = this.#adapter,
134 state = this.#state,
135 graph = adapter.graph;
136 const key = adapter.fileKey(path);
137 const file = key === undefined ? undefined : adapter.markers.get(state.locator, key);
138 if (!file) return [];
139 const result: SeqLine[] = [];
140 for (const node of adapter.sequence.range(
141 state.sequence,
142 adapter.sequence.rank(state.sequence, file.enter) + 1,
143 adapter.sequence.rank(state.sequence, file.exit),
144 false,
145 )) {
146 if (node.text.endsWith(":end")) continue;
147 const n = graph.lineN(node.text),
148 writer = graph.writerSeqOf(n);
149 if (!adapter.members.get(state.membership, writer)?.present) continue;
150 const deletedBy = [...graph.eachDeleterSeqOf(n)]
151 .filter((seq) => adapter.members.get(state.membership, seq)?.present)
152 .sort((a, b) => graph.lamportOfSeq(a) - graph.lamportOfSeq(b) || a - b)
153 .map((seq) => graph.idOfSeq(seq));
154 result.push({
155 id: node.text,
156 text: graph.textOf(n),
157 atom: graph.idOfSeq(writer),
158 deletedBy,
159 visible: deletedBy.length === 0,
160 });
161 }
162 return result;
163 }
164 sequenceIndex(path: string, line: string): number {
165 this.#assertLive();
166 return this.sequence(path).findIndex((item) => item.id === line);
167 }
168 parentOf(line: string): string {
169 this.#assertLive();
170 const adapter = this.#adapter,
171 state = this.#state;
172 const marker = adapter.markers.get(state.locator, adapter.graph.lineN(line) + 1);
173 if (marker?.kind !== "line") throw Error("missing projected line");
174 if (marker.parentKey > 0 && marker.parentKey < 0x80000000) return adapter.graph.lineIdOf(marker.parentKey - 1);
175 const parent = adapter.markers.get(state.locator, marker.parentKey);
176 if (parent?.kind !== "file") throw Error("missing projected file parent");
177 const text = adapter.sequence.get(state.sequence.map, parent.enter).text;
178 if (!text.startsWith("root:")) throw Error("invalid projected file parent");
179 return startNode(text.slice(5));
180 }
181 fileOf(line: string): string {
182 this.#assertLive();
183 if (line.startsWith("^")) return line.slice(1);
184 let current = line;
185 while (!current.startsWith("^")) {
186 const n = this.graph.lineN(current);
187 if (n < 0) throw Error("unknown projected line");
188 const marker = this.#adapter.markers.get(this.#state.locator, n + 1);
189 if (marker?.kind === "line") return this.#adapter.sequence.fileAt(this.#state.sequence, marker.enter);
190 // A line written outside this view has no marker. Follow canonical ancestry
191 // until it reaches a line whose placement this view actually contains.
192 if (marker || this.#adapter.members.get(this.#state.membership, this.graph.writerSeqOf(n))?.present)
193 throw Error("missing projected line");
194 const parent = this.graph.lineParent(current);
195 if (parent === undefined) throw Error("missing projected line parent");
196 current = parent;
197 }
198 return current.slice(1);
199 }
200 text(path: string): string {
201 this.#assertLive();
202 let text = "",
203 part = "";
204 for (const line of this.visibleLines(path)) {
205 part += `${line.text}\n`;
206 if (part.length >= 16384) {
207 text += part;
208 part = "";
209 }
210 }
211 return text + part;
212 }
213 hasFile(path: string): boolean {
214 this.#assertLive();
215 const attrs = this.attrs(path);
216 return (
217 attrs.exists ||
218 attrs.link !== null ||
219 attrs.blob !== null ||
220 !this.#adapter.lineIds(this.#state, path).next().done
221 );
222 }