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
196 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 158, 2 versions changed it

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

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