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/where.ts?version=295&format=json
128 linesFirst import 63% · capacity 38%Last change capacity in version 212, 3 versions changed it

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

1// Who's working where (slice 2 of #18): for each file, the agents whose
2// drafts touch it, which lines, and when they last edited. Lines are given
3// two ways: in the agent's own view (what its folder shows), and in shared's
4// numbering (the shared lines it changes, or the shared line its new lines
5// follow), so agents can compare where they are against one common text.
6import { type AtomId, type LineId, type ReadView, seqOf } from "../../src/engine/index.ts";
7import type { Project } from "./project.ts";
8
9type Range = [number, number];
11/** A view's reads by line number (View has them; another ReadView may not). */
12type ByNumber = ReadView & {
13 fileOfNumber?(n: number): string;
14 lineNumberOf?(path: string, n: number): number;
15 visibleWriters?(path: string): readonly number[];
16};
18function ranges(ns: number[]): Range[] {
19 const out: Range[] = [];
20 for (const n of [...new Set(ns)].sort((a, b) => a - b)) {
21 const last = out.at(-1);
22 if (last && n === last[1] + 1) last[1] = n;
23 else out.push([n, n]);
24 }
25 return out;
28export interface Spot {
29 agent: string;
30 /** Line ranges in the agent's own view. */
31 lines: Range[];
32 /** Line ranges in shared: lines it deletes or rewrites, and the lines its new lines come after. */
33 shared: Range[];
34 changes: number;
35 lastEdit: number;
38export function where(project: Project) {
39 const graph = project.graph;
40 let shared: ByNumber | undefined;
41 // By the file's index of visible lines (abso Capacity): finding it in lines() built every line of the file again
42 // whenever the file changed, per line asked.
43 const sharedNumber = (file: string, line: LineId | number): number | undefined => {
44 shared ??= project.sharedView() as ByNumber;
45 if (!shared.hasFile(file)) return undefined;
46 const i =
47 typeof line === "string"
48 ? shared.lineNumber(file, line)
49 : shared.lineNumberOf
50 ? shared.lineNumberOf(file, line)
51 : shared.lineNumber(file, graph.lineIdOf(line));
52 return i >= 0 ? i + 1 : undefined;
53 };
54 const files: Record<string, Spot[]> = {};
55 for (const agent of project.agents()) {
56 const draft = project.draftOf(agent);
57 if (!draft || draft.atoms.length === 0) continue;
58 const mine = new Set<AtomId>(draft.atoms);
59 // Once per agent (abso Capacity): each read of draft.view asks the project's view cache, which weighs every
60 // view it holds.
61 const view = draft.view as ByNumber;
62 const fileOfNumber = view.fileOfNumber?.bind(view);
63 const perFile = new Map<string, { view: number[]; shared: number[]; changes: Set<AtomId>; last: number }>();
64 const at = (file: string) => {
65 let s = perFile.get(file);
66 if (!s) perFile.set(file, (s = { view: [], shared: [], changes: new Set(), last: 0 }));
67 return s;
68 };
69 for (const id of draft.atoms) {
70 const atom = graph.atom(id);
71 for (const run of atom.inserts) {
72 // A line moved by someone (#28) is in the file the draft's view puts it in.
73 const file = run.parent.startsWith("^") ? run.file : view.fileOf(run.parent);
74 const s = at(file);
75 s.changes.add(id);
76 s.last = Math.max(s.last, atom.time);
77 // The shared line its new lines follow ("^path" is the top of the file: line 0).
78 const anchor = run.parent.startsWith("^") ? 0 : sharedNumber(file, run.parent);
79 if (anchor !== undefined) s.shared.push(Math.max(anchor, 1));
80 }
81 // By number where the view can (abso Capacity): a draft deleting a big generated file read every line's text
82 // to check it exists, and looked up each line's id.
83 if (fileOfNumber) {
84 for (const line of graph.deletedNumbers(id)) {
85 const file = fileOfNumber(line);
86 const s = at(file);
87 s.changes.add(id);
88 s.last = Math.max(s.last, atom.time);
89 const n = sharedNumber(file, line);
90 if (n !== undefined) s.shared.push(n);
91 }
92 continue;
93 }
94 for (const lineId of atom.deletes) {
95 const info = graph.line(lineId);
96 if (!info) continue;
97 const file = view.fileOf(lineId);
98 const s = at(file);
99 s.changes.add(id);
100 s.last = Math.max(s.last, atom.time);
101 const n = sharedNumber(file, lineId);
102 if (n !== undefined) s.shared.push(n);
103 }
104 }
105 for (const [file, s] of perFile) {
106 if (view.hasFile(file)) {
107 if (view.visibleWriters) {
108 const seqs = new Set(draft.atoms.map(seqOf));
109 view.visibleWriters(file).forEach((seq, i) => {
110 if (seqs.has(seq)) s.view.push(i + 1);
111 });
112 } else
113 view.lines(file).forEach((l, i) => {
114 if (mine.has(l.atom)) s.view.push(i + 1);
115 });
116 }
117 (files[file] ??= []).push({
118 agent,
119 lines: ranges(s.view),
120 shared: ranges(s.shared),
121 changes: s.changes.size,
122 lastEdit: s.last,
123 });
124 }
125 }
126 // What agents said they are starting on (#37, #13), whether or not an edit has landed yet.
127 return { version: project.version, files, starting: project.startingNotes() };