- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/where.ts?draft=login-timeout&format=json
90 linesFirst import 100%0 versions changed it
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, LineId } from "../../src/engine/index.ts";
7import type { Project } from "./project.ts";
9type Range = [number, number];
11function ranges(ns: number[]): Range[] {
12 const out: Range[] = [];
13 for (const n of [...new Set(ns)].sort((a, b) => a - b)) {
14 const last = out.at(-1);
15 if (last && n === last[1] + 1) last[1] = n;
16 else out.push([n, n]);
17 }
18 return out;
19}
21export interface Spot {
22 agent: string;
23 /** Line ranges in the agent's own view. */
24 lines: Range[];
25 /** Line ranges in shared: lines it deletes or rewrites, and the lines its new lines come after. */
26 shared: Range[];
27 changes: number;
28 lastEdit: number;
29}
31export function where(project: Project) {
32 const graph = project.graph;
33 const shared = project.sharedView();
34 const sharedNumber = (file: string, id: LineId): number | undefined => {
35 if (!shared.hasFile(file)) return undefined;
36 const i = shared.lines(file).findIndex((l) => l.id === id);
37 return i >= 0 ? i + 1 : undefined;
38 };
39 const files: Record<string, Spot[]> = {};
40 for (const agent of project.agents()) {
41 const draft = project.draftOf(agent);
42 if (!draft || draft.atoms.length === 0) continue;
43 const mine = new Set<AtomId>(draft.atoms);
44 const perFile = new Map<string, { view: number[]; shared: number[]; changes: Set<AtomId>; last: number }>();
45 const at = (file: string) => {
46 let s = perFile.get(file);
47 if (!s) perFile.set(file, (s = { view: [], shared: [], changes: new Set(), last: 0 }));
48 return s;
49 };
50 for (const id of draft.atoms) {
51 const atom = graph.atom(id);
52 for (const run of atom.inserts) {
53 // A line moved by someone (#28) is in the file the draft's view puts it in.
54 const file = run.parent.startsWith("^") ? run.file : draft.view.fileOf(run.parent);
55 const s = at(file);
56 s.changes.add(id);
57 s.last = Math.max(s.last, atom.time);
58 // The shared line its new lines follow ("^path" is the top of the file: line 0).
59 const anchor = run.parent.startsWith("^") ? 0 : sharedNumber(file, run.parent);
60 if (anchor !== undefined) s.shared.push(Math.max(anchor, 1));
61 }
62 for (const lineId of atom.deletes) {
63 const info = graph.line(lineId);
64 if (!info) continue;
65 const file = draft.view.fileOf(lineId);
66 const s = at(file);
67 s.changes.add(id);
68 s.last = Math.max(s.last, atom.time);
69 const n = sharedNumber(file, lineId);
70 if (n !== undefined) s.shared.push(n);
71 }
72 }
73 for (const [file, s] of perFile) {
74 if (draft.view.hasFile(file)) {
75 draft.view.lines(file).forEach((l, i) => {
76 if (mine.has(l.atom)) s.view.push(i + 1);
77 });
78 }
79 (files[file] ??= []).push({
80 agent,
81 lines: ranges(s.view),
82 shared: ranges(s.shared),
83 changes: s.changes.size,
84 lastEdit: s.last,
85 });
86 }
87 }
88 // What agents said they are starting on (#37, #13), whether or not an edit has landed yet.
89 return { version: project.version, files, starting: project.startingNotes() };
90}