- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/notes.ts?version=278&format=json
326 linesFirst import 95% · codex-capacity-chair 4% · infra-split 1%Last change codex-capacity-chair in version 143, 4 versions changed it
You're reading version 278. The newest is 318, and no version since then changed this file.
1// Notes on lines and review areas (#44): where a note's lines are now and what that makes it, who
2// reviews which files by avcs.json, the notes list, every agent's start note, and writing or closing
3// a note. Reads go through NoteHost, writes through NoteWriteHost; today the host is Project.
5import type { AtomId, Graph, LineId, ReadView } from "../../src/engine/index.ts";
6import { matchesPattern } from "./globs.ts";
7import { type NewsItem, noteClosedItem, noteSaid } from "./news.ts";
8import { checkReadPath } from "./path-rules.ts";
9import { ProjectError } from "./project-error.ts";
10import type {
11 AgentState,
12 ChangeRecord,
13 Entry,
14 NoteAnswer,
15 NoteBody,
16 NoteRecord,
17 PersonSender,
18 Starting,
19} from "./project-types.ts";
20import { last, SHARED, type Space } from "./spaces.ts";
22/** What the note reads need from a project. */
23export interface NoteHost {
24 readonly graph: Graph;
25 readonly agents: ReadonlyMap<string, AgentState>;
26 readonly spaces: ReadonlyMap<string, Space>;
27 readonly notes: ReadonlyMap<number, NoteRecord>;
28 initialized(): boolean;
29 shared(): Space;
30 sharedView(): ReadView;
31 spellingIn(view: ReadView, path: string): string;
32 viewAt(space: Space, version: number): ReadView;
33 agentView(agent: AgentState): ReadView;
34 view(set: ReadonlySet<AtomId>): ReadView;
35 at(space: Space, version: number): ReadonlySet<AtomId>;
36 liveNote(agent: AgentState): Starting | undefined;
37}
39/** What writing or closing a note needs from a project. */
40export interface NoteWriteHost {
41 readonly notes: Map<number, NoteRecord>;
42 readonly agents: ReadonlyMap<string, AgentState>;
43 readonly changes: ReadonlyMap<number, ChangeRecord>;
44 readonly graph: Graph;
45 lastNote(): number;
46 agentView(agent: AgentState): ReadView;
47 viewAtVersion(version: number): ReadView;
48 sharedView(): ReadView;
49 changeOfLines(lines: readonly LineId[]): number | undefined;
50 apply(entry: Entry): void;
51 flush(entries: Entry[]): void;
52 queueNews(agent: AgentState, items: NewsItem[], entries: Entry[]): void;
53 noteAnswer(n: NoteRecord): NoteAnswer;
54}
56export interface ReviewRule {
57 readonly paths: string[];
58 readonly reviewer: string;
59}
61/** The project's reviewers per area: avcs.json's `"review": [{"paths": [...], "reviewer": "..."}]`. */
62export function reviewRulesIn(avcsJson: string | undefined): ReviewRule[] {
63 if (avcsJson === undefined) return [];
64 let review: unknown;
65 try {
66 review = (JSON.parse(avcsJson) as { review?: unknown }).review;
67 } catch {
68 return [];
69 }
70 if (!Array.isArray(review)) return [];
71 return review.flatMap((r) => {
72 const { paths, reviewer } = (r ?? {}) as { paths?: unknown; reviewer?: unknown };
73 return Array.isArray(paths) &&
74 paths.every((p) => typeof p === "string") &&
75 typeof reviewer === "string" &&
76 reviewer !== ""
77 ? [{ paths: paths as string[], reviewer }]
78 : [];
79 });
80}
82/** Each reviewer whose area these files fall in, the files that do, and whether it has noted the change. */
83export function reviewersOf(
84 rules: readonly ReviewRule[],
85 files: readonly string[],
86 hasNoted: (agent: string) => boolean,
87): { agent: string; files: string[]; read: boolean }[] {
88 const out = new Map<string, Set<string>>();
89 for (const rule of rules) {
90 const matched = files.filter((f) => rule.paths.some((p) => matchesPattern(p, f)));
91 if (matched.length === 0) continue;
92 const set = out.get(rule.reviewer) ?? new Set<string>();
93 for (const f of matched) set.add(f);
94 out.set(rule.reviewer, set);
95 }
96 return [...out]
97 .sort(([a], [b]) => (a < b ? -1 : 1))
98 .map(([agent, set]) => ({ agent, files: [...set].sort(), read: hasNoted(agent) }));
99}
101/** A note as reads show it, from the view its lines live in (undefined when no space holds them any more). */
102export function noteAnswerIn(n: NoteRecord, view: ReadView | undefined): NoteAnswer {
103 // Where the lines are now, found by their ids: a move (#49) can have taken them to another file.
104 let lines: [number, number] | null = null;
105 let file = n.file;
106 if (view) {
107 const found: { file: string; at: number }[] = [];
108 for (const id of n.lines) {
109 const f = view.fileOf(id);
110 const at = view.lineNumber(f, id);
111 if (at >= 0) found.push({ file: f, at: at + 1 });
112 }
113 if (found.length > 0) {
114 file = found[0]!.file;
115 const at = found.filter((x) => x.file === file).map((x) => x.at);
116 lines = [Math.min(...at), Math.max(...at)];
117 }
118 }
119 const state = n.closed ? n.closed.how : lines !== null ? "open" : n.kind === "objection" ? "addressed" : "outdated";
120 return {
121 id: n.id,
122 agent: n.agent,
123 kind: n.kind,
124 file,
125 lines,
126 text: n.text,
127 change: n.change ?? null,
128 reply: n.reply ?? null,
129 time: n.time,
130 state,
131 ...(n.person ? { person: { ...n.person.person } } : {}),
132 };
133}
135/**
136 * The view a note's lines live in (#44):
137 * shared once any were published there; else the named draft they were
138 * published into; else their author's draft, where it works now or where it
139 * parked it. Undefined when no space holds them any more (a discarded draft).
140 */
141export function noteView(host: NoteHost, n: NoteRecord): ReadView | undefined {
142 const writers = new Set(n.lines.map((l) => host.graph.lineAtom(l)).filter((a): a is AtomId => a !== undefined));
143 const holds = (set: ReadonlySet<AtomId> | readonly AtomId[]) =>
144 [...writers].some((a) => (set instanceof Set ? set.has(a) : (set as readonly AtomId[]).includes(a)));
145 if (holds(host.shared().own)) return host.sharedView();
146 for (const space of host.spaces.values())
147 if (space !== host.shared() && holds(space.own)) return host.viewAt(space, last(space));
148 const first = [...writers][0];
149 const author = first === undefined ? undefined : host.agents.get(host.graph.atom(first).author);
150 if (!author) return undefined;
151 if (holds(author.mine)) return host.agentView(author);
152 for (const [name, parked] of author.parked) {
153 const space = host.spaces.get(name);
154 if (space && holds(parked.mine)) return host.view(new Set([...host.at(space, parked.base), ...parked.mine]));
155 }
156 return undefined;
157}
159export function noteAnswer(host: NoteHost, n: NoteRecord): NoteAnswer {
160 return noteAnswerIn(n, noteView(host, n));
161}
163/** Notes, oldest first: on a file, on a change, or only the ones still open. */
164export function notes(
165 host: NoteHost,
166 options: { file?: string; change?: number; open?: boolean } = {},
167): { notes: NoteAnswer[] } {
168 // Any spelling reads the stored one (#41, F7).
169 const file =
170 options.file === undefined || !host.initialized() ? options.file : host.spellingIn(host.sharedView(), options.file);
171 const out = [...host.notes.values()]
172 .filter((n) => options.change === undefined || n.change === options.change)
173 .map((n) => ({ n, a: noteAnswer(host, n) }))
174 .filter(({ n, a }) => file === undefined || a.file === file || n.file === file)
175 .map(({ a }) => a)
176 .filter((n) => !options.open || n.state === "open");
177 return { notes: out };
178}
180/** Every agent's starting note, by agent name. */
181export function startingNotes(
182 host: NoteHost,
183): { agent: string; draft: string | null; text: string; files: string[]; time: number }[] {
184 return [...host.agents.values()]
185 .filter((a) => host.liveNote(a))
186 .toSorted((a, b) => (a.name < b.name ? -1 : 1))
187 .map((a) => {
188 const note = host.liveNote(a)!;
189 return { agent: a.name, draft: a.space === SHARED ? null : a.space, ...note, files: [...note.files] };
190 });
191}
193export function writeNote(
194 host: NoteWriteHost,
195 entries: Entry[],
196 writerOf: () => { name: string; person?: PersonSender },
197 body: NoteBody,
198 time: number,
199): { id: number; change: number | null; lines: [number, number] } {
200 const text = body.text;
201 if (typeof text !== "string" || text.trim() === "") throw new ProjectError("bad-request", "a note needs {text}");
202 if (text.length > 4000) throw new ProjectError("bad-request", "a note is at most 4000 characters");
203 const kind = body.kind ?? "comment";
204 if (kind !== "comment" && kind !== "objection" && kind !== "read")
205 throw new ProjectError("bad-request", "kind is comment, objection or read");
206 let file: string;
207 let lines: LineId[];
208 let change: number | undefined;
209 let reply: number | undefined;
210 if (body.reply !== undefined) {
211 const parent = Number.isInteger(body.reply) ? host.notes.get(body.reply as number) : undefined;
212 if (!parent) throw new ProjectError("not-found", `there is no note ${String(body.reply)}`);
213 reply = parent.reply ?? parent.id;
214 ({ file, change } = parent);
215 lines = [...parent.lines];
216 } else {
217 if (typeof body.file !== "string")
218 throw new ProjectError("bad-request", "a note needs {file, from, to}, or {reply}");
219 file = body.file;
220 checkReadPath(file);
221 let view: ReadView;
222 if (typeof body.of === "string") {
223 const of = host.agents.get(body.of);
224 if (!of) throw new ProjectError("not-found", `there is no agent ${JSON.stringify(body.of)}`);
225 view = host.agentView(of);
226 } else if (body.version !== undefined) {
227 view = host.viewAtVersion(body.version as number);
228 } else {
229 view = host.sharedView();
230 }
231 if (!view.hasFile(file)) throw new ProjectError("not-found", `there is no ${file} there`);
232 const shown = view.lines(file);
233 const from = body.from ?? 1;
234 const to = body.to ?? from;
235 if (
236 !Number.isInteger(from) ||
237 !Number.isInteger(to) ||
238 (from as number) < 1 ||
239 (to as number) < (from as number) ||
240 (to as number) > shown.length
241 ) {
242 throw new ProjectError("bad-request", `from and to are line numbers in ${file}, 1 to ${shown.length}`);
243 }
244 if ((to as number) - (from as number) >= 500)
245 throw new ProjectError("bad-request", "a note covers at most 500 lines");
246 lines = shown.slice((from as number) - 1, to as number).map((l) => l.id);
247 change = host.changeOfLines(lines);
248 }
249 const writer = writerOf();
250 const id = host.lastNote() + 1;
251 const entry: Entry = {
252 type: "note",
253 id,
254 agent: writer.name,
255 kind,
256 file,
257 lines,
258 text: text.trim(),
259 ...(change !== undefined ? { change } : {}),
260 ...(reply !== undefined ? { reply } : {}),
261 time,
262 ...(writer.person ? { person: writer.person } : {}),
263 };
264 entries.push(entry);
265 host.apply(entry);
266 const note = host.notes.get(id)!;
267 const where = host.noteAnswer(note).lines;
268 if (kind !== "read") {
269 const rec = change !== undefined ? host.changes.get(change) : undefined;
270 const hearers = new Set<string>();
271 for (const l of lines) {
272 const info = host.graph.line(l);
273 if (info) hearers.add(host.graph.atom(info.atom).author);
274 }
275 if (rec) hearers.add(rec.agent);
276 if (reply !== undefined)
277 for (const n of host.notes.values()) if (n.id === reply || n.reply === reply) hearers.add(n.agent);
278 hearers.delete(writer.name);
279 const said = noteSaid({
280 id,
281 writer: writer.name,
282 ...(writer.person ? { person: writer.person } : {}),
283 kind,
284 reply: reply !== undefined,
285 file,
286 where,
287 ...(rec ? { change: rec.note?.text ?? rec.versions[0]?.message ?? "" } : {}),
288 text: text.trim(),
289 });
290 for (const name of [...hearers].toSorted()) {
291 const a = host.agents.get(name);
292 if (a) host.queueNews(a, [{ key: `note ${id}`, text: said }], entries);
293 }
294 }
295 host.flush(entries);
296 return { id, change: change ?? null, lines: where ?? [0, 0] };
297}
299/** Close a note: only its writer can (#44). */
300export function closeNote(
301 host: NoteWriteHost,
302 agentName: string,
303 id: number,
304 how: unknown,
305 time: number,
306 person?: PersonSender,
307): NoteAnswer {
308 const note = Number.isInteger(id) ? host.notes.get(id) : undefined;
309 if (!note) throw new ProjectError("not-found", `there is no note ${String(id)}`);
310 if (how !== "resolved" && how !== "withdrawn") throw new ProjectError("bad-request", "how is resolved or withdrawn");
311 if (note.agent !== agentName)
312 throw new ProjectError("conflict", `only ${note.agent}, who wrote note ${id}, can close it`);
313 if (note.closed) return host.noteAnswer(note);
314 const entries: Entry[] = [];
315 const entry: Entry = { type: "note-closed", id, agent: agentName, how, time, ...(person ? { person } : {}) };
316 entries.push(entry);
317 host.apply(entry);
318 const rec = note.change !== undefined ? host.changes.get(note.change) : undefined;
319 if (rec && rec.agent !== agentName && note.kind === "objection") {
320 const a = host.agents.get(rec.agent);
321 if (a) host.queueNews(a, [noteClosedItem(agentName, how, id, note)], entries);
322 }
323 host.flush(entries);
324 // Storage-backed maps return snapshots; apply may replace the stored row.
325 return host.noteAnswer(host.notes.get(id)!);
326}