- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/overlaps.ts?format=json
109 linesFirst import 85% · engine 7% · capacity 4% · codex-capacity-chair 2% · infra-split 2%Last change capacity in version 212, 5 versions changed it
1// Other agents whose unpublished changes or starting notes meet a file, and
2// the news when a draft first touches one. Reads and news writes go through
3// OverlapHost; today the host is Project. overlapsOf is the pure match.
5import { type AtomId, type Graph, isStartNode, type ReadView } from "../../src/engine/index.ts";
6import { matchesPattern, pathsInText, patternsMeet } from "./globs.ts";
7import { type NewsItem, overlapItems } from "./news.ts";
8import type { AgentState, Entry, Overlap, Starting } from "./project-types.ts";
10/** Another agent in the same space, as overlaps see it. */
11export interface OverlapPeer {
12 name: string;
13 /** Files its unpublished changes touch. */
14 files: ReadonlySet<string>;
15 /** How many of its unpublished changes touch `file`. */
16 changesIn: (file: string) => number;
17 /** Its live starting note. */
18 note: Starting | undefined;
19}
21/** What draft-file overlaps and their news need from a project. */
22export interface OverlapHost {
23 readonly graph: Graph;
24 readonly agents: ReadonlyMap<string, AgentState>;
25 agentView(agent: AgentState): ReadView;
26 draft(agent: AgentState): readonly AtomId[];
27 /** draftFiles below, kept per agent by the host. */
28 draftFiles(agent: AgentState): Set<string>;
29 liveNote(agent: AgentState): Starting | undefined;
30 queueNews(agent: AgentState, items: readonly NewsItem[], entries: Entry[]): void;
31}
33/** What a starting note can name: its files (globs included) and the paths written in its text. */
34export const notePatterns = (note: Starting | undefined): string[] =>
35 note ? [...new Set([...note.files, ...pathsInText(note.text)])] : [];
37/**
38 * Peers with unpublished changes in a file `patterns` match, or a starting
39 * note that can name the same file (#37: notes name files by glob,
40 * or only in their text).
41 */
42export function overlapsOf(peers: readonly OverlapPeer[], patterns: readonly string[]): Overlap[] {
43 if (patterns.length === 0) return [];
44 const out: Overlap[] = [];
45 for (const other of peers.toSorted((a, b) => (a.name < b.name ? -1 : 1))) {
46 const found = new Map<string, Overlap>();
47 for (const file of [...other.files].toSorted()) {
48 if (!patterns.some((p) => matchesPattern(p, file))) continue;
49 found.set(file, { agent: other.name, file, changes: other.changesIn(file) });
50 }
51 for (const theirs of notePatterns(other.note)) {
52 for (const p of patterns) {
53 if (!patternsMeet(p, theirs)) continue;
54 // Name the file as plainly as either side gives it.
55 const file = /[*?]/.test(p) ? theirs : p;
56 found.set(file, found.get(file) ?? { agent: other.name, file });
57 }
58 }
59 for (const o of [...found.values()].toSorted((a, b) => (a.file < b.file ? -1 : 1))) {
60 out.push({ ...o, ...(other.note ? { starting: other.note.text } : {}) });
61 }
62 }
63 return out;
64}
66/** The files the agent's unpublished changes touch, in its own view. */
67export function draftFiles(host: OverlapHost, agent: AgentState): Set<string> {
68 const files = new Set<string>();
69 const draft = host.draft(agent);
70 // No draft, no view to build: most agents in a big project have nothing unpublished.
71 if (draft.length === 0) return files;
72 const view = host.agentView(agent);
73 // By number where the view can (abso Capacity): a draft deleting a big generated file looked up each line's id.
74 const byNumber = (view as { fileOfNumber?(n: number): string }).fileOfNumber?.bind(view);
75 for (const id of draft) {
76 const atom = host.graph.atom(id);
77 for (const run of atom.inserts) files.add(isStartNode(run.parent) ? run.file : view.fileOf(run.parent));
78 if (byNumber) for (const n of host.graph.deletedNumbers(id)) files.add(byNumber(n));
79 else for (const line of atom.deletes) files.add(view.fileOf(line));
80 for (const m of atom.moves ?? []) files.add(view.fileOf(m.line));
81 }
82 return files;
83}
85/** Other agents in the agent's space whose unpublished changes or starting note meet `patterns`. */
86export function overlaps(host: OverlapHost, agent: AgentState, patterns: readonly string[]): Overlap[] {
87 if (patterns.length === 0) return [];
88 const peers = [...host.agents.values()]
89 .filter((other) => other !== agent && other.space === agent.space)
90 .map((other) => ({
91 name: other.name,
92 files: host.draftFiles(other),
93 changesIn: (file: string) => host.draft(other).filter((id) => host.graph.filesOf(id).includes(file)).length,
94 note: host.liveNote(other),
95 }));
96 return overlapsOf(peers, patterns);
97}
99/**
100 * News the first time a draft touches a file someone else in its space has
101 * unpublished changes in, or a starting note on (#37): one line per file and agent, once per draft. It tells; it never stops
102 * the edit.
103 */
104export function overlapNews(host: OverlapHost, agent: AgentState, before: ReadonlySet<string>, entries: Entry[]): void {
105 const now = host.draftFiles(agent);
106 const fresh = [...now].filter((f) => !before.has(f)).toSorted();
107 if (fresh.length === 0) return;
108 host.queueNews(agent, overlapItems(host.draft(agent)[0] ?? "", overlaps(host, agent, fresh)), entries);
109}