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/ingest.ts?version=315&format=json
152 linesFirst import 93% · engine 4% · codex-capacity-chair 3%Last change codex-capacity-chair in version 143, 3 versions changed it

You're reading version 315. The newest is 318, and no version since then changed this file. Go to the newestCompare with 318

1// Take in commits a person pushed with plain git. Writes go through IngestHost; today the host is Project.
2
3import { type AtomId, canonicalText, type Hunk, type ReadView } from "../../src/engine/index.ts";
4import { checkPath } from "./path-rules.ts";
5import { ProjectError } from "./project-error.ts";
6import { configProblem, PROJECT_FILES } from "./project-helpers.ts";
7import type { AgentState, Entry, IngestAnswer, LandAnswer, Meta, Mode, Modes, PublishAnswer } from "./project-types.ts";
8import type { Space } from "./spaces.ts";
9import { clashInPaths } from "./spelling.ts";
11/** What taking in a git push needs from a project. */
12export interface IngestHost {
13 readonly agents: ReadonlyMap<string, AgentState>;
14 readonly spaces: ReadonlyMap<string, Space>;
15 needInit(): void;
16 version(): number;
17 checkBlobs(blobs: unknown, files: object): Record<string, string>;
18 apply(entry: Entry): void;
19 flush(entries: Entry[]): void;
20 wholeOut(
21 files: Readonly<Record<string, string | null>>,
22 blobs: Readonly<Record<string, string>>,
23 view: ReadView | undefined,
24 config?: string,
25 ): { files: Record<string, string | null>; blobs: Record<string, string> };
26 agentView(agent: AgentState): ReadView;
27 recordFile(
28 agent: AgentState,
29 view: ReadView,
30 path: string,
31 text: string | null | undefined,
32 time: number,
33 meta: Meta,
34 entries: Entry[],
35 mode?: Mode,
36 blob?: string,
37 hunks?: readonly Hunk[],
38 ): boolean;
39 publish(
40 agent: AgentState,
41 message: string,
42 time: number,
43 entries: Entry[],
44 asIs?: boolean,
45 direct?: boolean,
46 ): PublishAnswer;
47 land(
48 agent: AgentState,
49 message: string,
50 time: number,
51 entries: Entry[],
52 pick?: { space: Space; changes: AtomId[] },
53 ): LandAnswer;
56/**
57 * Take in commits a person pushed with plain git (demo moment 5, #5, #8).
58 * `base` is the shared version their first commit builds on (the
59 * `avcs-version` of the commit they cloned). Each commit gives its whole
60 * tree as text; a file of the view missing from it is deleted. The commits
61 * go into a named draft `git-<sha>` on that version, one publish each, and
62 * the draft lands in shared at once if nothing collides. The push already
63 * happened, so a collision isn't refused: the draft waits, with its
64 * collisions listed, for someone to switch to it, settle and land it.
65 */
66export function ingest(
67 host: IngestHost,
68 input: {
69 author: string;
70 base: number;
71 commits: readonly {
72 sha: string;
73 message: string;
74 files: Readonly<Record<string, string>>;
75 modes?: Modes;
76 blobs?: Readonly<Record<string, string>>;
77 }[];
78 },
79 time: number,
80): IngestAnswer {
81 host.needInit();
82 if (typeof input !== "object" || input === null || !Array.isArray(input.commits) || input.commits.length === 0) {
83 throw new ProjectError("bad-request", "ingest needs {author, base, commits: [{sha, message, files}]}");
84 }
85 if (!Number.isInteger(input.base) || input.base < 0 || input.base > host.version()) {
86 throw new ProjectError("bad-request", `base ${String(input.base)} is not a shared version`);
87 }
88 for (const c of input.commits) {
89 if (typeof c?.sha !== "string" || !/^[0-9a-f]{7,64}$/.test(c.sha))
90 throw new ProjectError("bad-request", "each commit needs a hex sha");
91 if (typeof c.message !== "string" || typeof c.files !== "object" || c.files === null) {
92 throw new ProjectError("bad-request", "each commit needs a message and files");
93 }
94 for (const path of Object.keys(c.files)) {
95 checkPath(path);
96 if (typeof c.files[path] !== "string")
97 throw new ProjectError("bad-request", `${path}: file text must be a string`);
98 }
99 if (c.blobs !== undefined) host.checkBlobs(c.blobs, c.files);
100 const clash = clashInPaths([...new Set([...Object.keys(c.files), ...Object.keys(c.blobs ?? {})])].toSorted());
101 if (clash)
102 throw new ProjectError(
103 "conflict",
104 `commit ${c.sha.slice(0, 12)}: ${clash} (#49); rename one with git mv and push again`,
105 );
106 for (const name of PROJECT_FILES) {
107 const config = typeof c.files[name] === "string" ? configProblem(c.files[name]) : undefined;
108 if (config)
109 throw new ProjectError("conflict", `commit ${c.sha.slice(0, 12)}: ${name} ${config}; fix it and push again`);
110 }
111 }
112 const name = `git-${input.commits[0]!.sha.slice(0, 12)}`;
113 if (host.agents.has(name) || host.spaces.has(name)) {
114 throw new ProjectError("bad-request", `commit ${input.commits[0]!.sha.slice(0, 12)} was already taken in`);
115 }
116 const entries: Entry[] = [];
117 for (const e of [
118 { type: "join", agent: name, version: input.base },
119 { type: "space", name, agent: name, time, sharedBase: input.base },
120 { type: "switch", agent: name, space: name, version: 0, carry: false },
121 ] as Entry[]) {
122 entries.push(e);
123 host.apply(e);
124 }
125 const agent = host.agents.get(name)!;
126 const author = typeof input.author === "string" && input.author.trim() !== "" ? input.author.trim() : "git";
127 const about = (c: { sha: string; message: string }): string =>
128 `${c.message.trim()}\n\nGit-Commit: ${c.sha}\nAuthor: ${author}`;
129 for (const original of input.commits) {
130 const out = host.wholeOut(original.files, original.blobs ?? {}, host.agentView(agent));
131 const commit = { ...original, files: out.files as Record<string, string> };
132 const blobs = out.blobs;
133 const paths = new Set([...host.agentView(agent).paths(), ...Object.keys(commit.files), ...Object.keys(blobs)]);
134 for (const path of [...paths].toSorted()) {
135 const text = commit.files[path];
136 const blob = blobs[path];
137 // With modes, the commit's whole set: a file not listed is plain. Without, modes stay as they were.
138 const mode: Mode | undefined = commit.modes
139 ? { exec: commit.modes[path]?.exec === true, link: commit.modes[path]?.link ?? null }
140 : undefined;
141 const target = blob !== undefined ? "" : text === undefined ? null : canonicalText(text);
142 host.recordFile(agent, host.agentView(agent), path, target, time, {}, entries, mode, blob);
143 }
144 // A git push can't answer a refusal mid-way, so each commit goes as it is, straight into its named draft.
145 host.publish(agent, about(commit), time, entries, true, true);
146 }
147 const landed = host.land(agent, about(input.commits[input.commits.length - 1]!), time, entries);
148 host.flush(entries);
149 return landed.ok
150 ? { landed: true, version: landed.version, draft: name, changes: landed.changes }
151 : { landed: false, version: host.version(), draft: name, collisions: landed.collisions };