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/start.ts?format=json
104 linesFirst import 99% · infra-split 1%Last change infra-split in version 37, 2 versions changed it
1// Say what an agent is starting on, before any edit lands. Writes go through
2// StartingHost; today the host is Project.
3
4import { type NewsItem, startingItem } from "./news.ts";
5import { notePatterns } from "./overlaps.ts";
6import { checkPath } from "./path-rules.ts";
7import { ProjectError } from "./project-error.ts";
8import type { AgentState, ChangeRecord, Entry, IssueHook, Overlap, Starting } from "./project-types.ts";
9
10/** What recording a start note needs from a project. */
11export interface StartingHost {
12 readonly agents: ReadonlyMap<string, AgentState>;
13 readonly changes: ReadonlyMap<number, ChangeRecord>;
14 agent(name: string, entries: Entry[]): AgentState;
15 liveNote(agent: AgentState): Starting | undefined;
16 apply(entry: Entry): void;
17 overlaps(agent: AgentState, patterns: readonly string[]): Overlap[];
18 queueNews(agent: AgentState, items: readonly NewsItem[], entries: Entry[]): void;
19 flush(entries: Entry[]): void;
22/**
23 * Say what the agent is starting on (#37, #13), before any edit lands:
24 * `text`, and the files it expects to change if it knows them. Nothing is
25 * locked or refused. where and status show it until the agent publishes,
26 * lands, or sends null. The answer names who already has unpublished
27 * changes in, or a starting note on, those files; they hear of it too.
28 */
29export function starting(
30 host: StartingHost,
31 agentName: string,
32 text: string | null,
33 files: readonly string[] | undefined,
34 time: number,
35 options: { issue?: unknown; done?: unknown } = {},
36): { starting: Starting | null; overlaps: Overlap[]; hooks?: { claim?: IssueHook; unclaim?: IssueHook } } {
37 const issue = options.issue;
38 if (issue !== undefined && (!Number.isInteger(issue) || (issue as number) < 1))
39 throw new ProjectError("bad-request", "issue is an issue number");
40 if (issue !== undefined && text === null)
41 throw new ProjectError("bad-request", "an issue goes with a start note, not a take-back");
42 if (options.done !== undefined && options.done !== true)
43 throw new ProjectError("bad-request", "done is true or left out");
44 if (options.done === true && text !== null)
45 throw new ProjectError("bad-request", "done goes with a take-back ({text: null})");
46 if (text !== null && (typeof text !== "string" || text.trim() === ""))
47 throw new ProjectError("bad-request", "starting needs {text}, or {text: null} to take it back");
48 if (typeof text === "string" && text.length > 1000)
49 throw new ProjectError("bad-request", "a starting note is at most 1000 characters");
50 if (files !== undefined && (!Array.isArray(files) || !files.every((f) => typeof f === "string"))) {
51 throw new ProjectError("bad-request", "files must be a list of paths");
52 }
53 if ((files?.length ?? 0) > 100) throw new ProjectError("bad-request", "a starting note names at most 100 files");
54 // Files a note names may be globs (#37): only the shape of a path is checked, not what a disk can hold.
55 for (const f of files ?? []) checkPath(f.replace(/[*?]/g, "x"));
56 const entries: Entry[] = [];
57 const agent = host.agent(agentName, entries);
58 const named = [...new Set(files ?? [])].toSorted();
59 // The note this one ends, if it named an issue (#54): its issue is no longer claimed by it.
60 const ending = host.liveNote(agent);
61 const endingAt = agent.noteAt;
62 const entry: Entry = {
63 type: "starting",
64 agent: agent.name,
65 text: text === null ? null : text.trim(),
66 ...(text !== null && named.length > 0 ? { files: named } : {}),
67 time,
68 ...(issue !== undefined ? { issue: issue as number } : {}),
69 ...(options.done === true ? { done: true as const } : {}),
70 };
71 entries.push(entry);
72 host.apply(entry);
73 const hooks: { claim?: IssueHook; unclaim?: IssueHook } = {};
74 if (ending?.issue !== undefined && endingAt !== undefined) {
75 // done names the change's newest version of shared, the one that finished the work.
76 const version = host.changes
77 .get(endingAt)
78 ?.versions.filter((v) => v.draft === null)
79 .at(-1)?.version;
80 hooks.unclaim = {
81 issue: ending.issue,
82 agent: agent.name,
83 change: endingAt,
84 done: options.done === true,
85 ...(version !== undefined ? { version } : {}),
86 };
87 }
88 if (issue !== undefined && agent.noteAt !== undefined)
89 hooks.claim = { issue: issue as number, agent: agent.name, change: agent.noteAt };
90 // Files named by glob, and paths written in the text, count too (#37).
91 const overlaps = text === null ? [] : host.overlaps(agent, notePatterns(host.liveNote(agent)));
92 for (const name of [...new Set(overlaps.map((o) => o.agent))]) {
93 const mine = overlaps.filter((o) => o.agent === name).map((o) => o.file);
94 host.queueNews(host.agents.get(name)!, [startingItem(agent.name, time, name, text!, mine)], entries);
95 }
96 host.flush(entries);
97 const note = host.liveNote(agent);
98 // hooks only when the Issues Worker has something to hear; the Worker takes them off the answer.
99 return {
100 starting: note ? { ...note, files: [...note.files] } : null,
101 overlaps,
102 ...(hooks.claim || hooks.unclaim ? { hooks } : {}),
103 };