- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/draft-collisions.ts?version=78&format=json
157 linesFirst import 99% · pinch 1%Last change pinch in version 78, 2 versions changed it
You're reading version 78. The newest is 318, and 5 versions since then changed this file.
1// Whether agents' unpublished changes would collide (#41, F22), read through DraftCollisionHost; today the host
2// is the project in memory.
4import { type AtomId, type Graph, parseKey, type View } from "../../src/engine/index.ts";
5import { describeSide, fileOfKey, type Pair } from "./collisions.ts";
6import type { DefinitionRule } from "./definition-rule.ts";
7import { type Definition, definedTwice } from "./definitions.ts";
8import { pathKey } from "./pathkey.ts";
9import type { CollisionSide, DraftCollision } from "./project-types.ts";
10import { last, SHARED, type Space } from "./spaces.ts";
12interface Agent {
13 readonly name: string;
14 readonly space: string;
15}
17/** What the draft collision read needs from a project. */
18export interface DraftCollisionHost {
19 readonly graph: Graph;
20 readonly agents: ReadonlyMap<string, Agent>;
21 needInit(): void;
22 shared(): Space;
23 sharedView(): View;
24 definitionRule(): DefinitionRule;
25 draft(agent: Agent): AtomId[];
26 agentView(agent: Agent): View;
27 view(set: ReadonlySet<AtomId>): View;
28 pairs(draft: readonly AtomId[], against: ReadonlySet<AtomId>, settled: ReadonlySet<AtomId>): Pair[];
29 touchedFiles(ids: Iterable<AtomId>, views: readonly View[]): Set<string>;
30}
32/**
33 * Whether agents' unpublished changes would collide (#41, F22), by the
34 * publish rule: for each pair of agents working in shared, one agent's
35 * changes as if published on shared now, then the other's checked against
36 * them, both ways round. Same lines, a definition one changes that the
37 * other's new lines use, the same attribute, and a name both define at the
38 * top of a file (refused once the second syncs onto the first's work). Exact for shared at `version`; as shared moves on, so can
39 * the answer, as a real publish's would. `file` or `folder` narrows it.
40 */
41export function draftCollisions(
42 host: DraftCollisionHost,
43 options: { file?: string; folder?: string } = {},
44): {
45 version: number;
46 agents: string[];
47 collisions: DraftCollision[];
48} {
49 host.needInit();
50 const working = [...host.agents.values()]
51 .filter((a) => a.space === SHARED && host.draft(a).length > 0)
52 .toSorted((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
53 const shared = host.shared().own;
54 const sharedView = host.sharedView();
55 const out: DraftCollision[] = [];
56 const side = (agent: string, file: string, s: CollisionSide) => ({ agent, file, lines: s.lines, text: s.text });
57 for (let i = 0; i < working.length; i++) {
58 for (let j = 0; j < working.length; j++) {
59 if (i === j) continue;
60 const a = working[i]!;
61 const b = working[j]!;
62 const draftA = host.draft(a);
63 const draftB = host.draft(b);
64 const newer = new Set(draftA);
65 const union = new Set([...shared, ...draftA, ...draftB]);
66 const viewA = host.agentView(a);
67 const viewB = host.agentView(b);
68 if (i < j) {
69 // Lines and attributes: the same either way round, so once per pair.
70 const paired = new Set<string>();
71 for (const pair of host.pairs(draftB, newer, union)) {
72 paired.add(`${pair.theirs} ${pair.yours}`);
73 const file = fileOfKey(pair.keys[0]!.key, viewB);
74 const fileKeys = pair.keys.filter((k) => fileOfKey(k.key, viewB) === file).map((k) => k.key);
75 const attrs = fileKeys.map((k) => parseKey(k)).filter((k) => k.kind === "attr");
76 const fileA = viewA.hasFile(file) ? file : fileOfKey(pair.keys[0]!.key, viewA);
77 if (attrs.length === fileKeys.length) {
78 const name = attrs[0]!.attr!;
79 const valueOf = (atom: AtomId) =>
80 (host.graph.atom(atom).attrs ?? []).find((x) => x.file === file && x.name === name)?.value ?? "";
81 out.push({
82 kind: "attribute",
83 name,
84 file,
85 first: { agent: a.name, file, lines: [1, 0], text: valueOf(pair.theirs) },
86 second: { agent: b.name, file, lines: [1, 0], text: valueOf(pair.yours) },
87 });
88 } else {
89 out.push({
90 kind: "lines",
91 file,
92 first: side(a.name, fileA, describeSide(viewA, pair.theirs, fileA, fileKeys)),
93 second: side(b.name, file, describeSide(viewB, pair.yours, file, fileKeys)),
94 });
95 }
96 }
97 // A name both define at the top level of one file: what the second meets once it syncs onto the
98 // first's work, as the publish rule checks it then. Not when the two definitions are one lines collision.
99 const both = host.view(union);
100 const mineA = new Set(draftA);
101 const mineB = new Set(draftB);
102 const filesA = host.touchedFiles(draftA, [viewA]);
103 for (const file of [...host.touchedFiles(draftB, [viewB])].filter((f) => filesA.has(f)).toSorted()) {
104 if (!both.hasFile(file)) continue;
105 for (const [name, defs] of definedTwice(both, file)) {
106 const by = (mine: Set<AtomId>) => defs.find((d) => d.lines.some((l) => mine.has(host.graph.line(l)!.atom)));
107 const dA = by(mineA);
108 const dB = by(mineB);
109 if (!dA || !dB || dA === dB) continue;
110 const atomsOf = (d: Definition, mine: Set<AtomId>) =>
111 d.lines.map((l) => host.graph.line(l)!.atom).filter((x) => mine.has(x));
112 if (atomsOf(dA, mineA).some((x) => atomsOf(dB, mineB).some((y) => paired.has(`${x} ${y}`)))) continue;
113 out.push({
114 kind: "duplicate",
115 name,
116 file,
117 first: { agent: a.name, file, lines: [dA.start + 1, dA.end], text: `${dA.text}\n` },
118 second: { agent: b.name, file, lines: [dB.start + 1, dB.end], text: `${dB.text}\n` },
119 });
120 }
121 }
122 }
123 // Definitions: b's new lines use a name whose definition a's changes change.
124 const nowView = host.view(new Set([...shared, ...draftA]));
125 for (const d of host.definitionRule().collisions(new Set(draftB), viewB, sharedView, nowView, newer, SHARED)) {
126 const c = d.collision;
127 out.push({
128 kind: "definition",
129 name: c.name!,
130 file: c.file,
131 first: { agent: a.name, file: c.theirs.file ?? c.file, lines: c.theirs.lines, text: c.theirs.text },
132 second: side(b.name, c.file, c.yours),
133 });
134 }
135 }
136 }
137 const fileKey = options.file !== undefined ? pathKey(options.file) : undefined;
138 const folderParts =
139 options.folder !== undefined ? options.folder.split("/").filter((s) => s !== "" && s !== ".") : undefined;
140 const under = (path: string): boolean => {
141 if (fileKey !== undefined && pathKey(path) !== fileKey) return false;
142 if (folderParts !== undefined && folderParts.length > 0) {
143 const parts = path.split("/");
144 if (
145 parts.length <= folderParts.length ||
146 pathKey(parts.slice(0, folderParts.length).join("/")) !== pathKey(folderParts.join("/"))
147 )
148 return false;
149 }
150 return true;
151 };
152 const narrowed =
153 fileKey === undefined && folderParts === undefined
154 ? out
155 : out.filter((c) => under(c.first.file) || under(c.second.file));
156 return { version: last(host.shared()), agents: working.map((a) => a.name), collisions: narrowed };
157}