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/record.ts?format=json
149 linesFirst import 96% · codex-capacity-chair 3% · infra-split 1%Last change codex-capacity-chair in version 143, 3 versions changed it
1// Whole files as the agent's folder has them. Writes go through RecordHost;
2// today the host is Project.
3
4import { canonicalText, type Hunk, type ReadView } from "../../src/engine/index.ts";
5import { checkPath } from "./path-rules.ts";
6import { isComposed, pathKey } from "./pathkey.ts";
7import { ProjectError } from "./project-error.ts";
8import type {
9 AgentState,
10 Entry,
11 Files,
12 Meta,
13 Mode,
14 Modes,
15 ProjectOptions,
16 RecordAnswer,
17 Spelling,
18} from "./project-types.ts";
19import { spellingOf } from "./spelling.ts";
20import { checkModes } from "./view-changes.ts";
22/** What recording whole files needs from a project. */
23export interface RecordHost {
24 readonly agents: ReadonlyMap<string, AgentState>;
25 readonly whole: ProjectOptions["whole"];
26 initialized(): boolean;
27 agentView(agent: AgentState): ReadView;
28 sharedView(): ReadView;
29 wholeOut(
30 files: Readonly<Record<string, string | null>>,
31 blobs: Readonly<Record<string, string>>,
32 view: ReadView | undefined,
33 config?: string,
34 ): { files: Record<string, string | null>; blobs: Record<string, string> };
35 checkBlobs(blobs: unknown, files: object): Record<string, string>;
36 agent(name: string, entries: Entry[]): AgentState;
37 draftFiles(agent: AgentState): Set<string>;
38 recordFile(
39 agent: AgentState,
40 view: ReadView,
41 path: string,
42 text: string | null | undefined,
43 time: number,
44 meta: Meta,
45 entries: Entry[],
46 mode?: Mode,
47 blob?: string,
48 hunks?: readonly Hunk[],
49 ): boolean;
50 overlapNews(agent: AgentState, before: ReadonlySet<string>, entries: Entry[]): void;
51 markedNews(agent: AgentState, files: readonly string[], entries: Entry[]): void;
52 newsAboutOwnDraft(agent: AgentState, entries: Entry[]): void;
53 takeNews(agent: AgentState, entries: Entry[]): string[];
54 flush(entries: Entry[]): void;
57/**
58 * Whole files as the agent's folder has them; each is diffed against its view.
59 */
60export function record(
61 host: RecordHost,
62 agentName: string,
63 files: Files,
64 time: number,
65 modes: Modes = {},
66 blobs: Readonly<Record<string, string>> = {},
67 meta: Meta = {},
68 options: { exactSpelling?: boolean } = {},
69): RecordAnswer {
70 if (typeof files !== "object" || files === null) throw new ProjectError("bad-request", "record needs {files}");
71 checkModes(modes);
72 // One file whatever the case of its spelling (#49): a second spelling is recorded as the existing one.
73 const spelling: Spelling[] = [];
74 if (!options.exactSpelling) {
75 const known = host.agents.get(agentName);
76 const view = known ? host.agentView(known) : host.initialized() ? host.sharedView() : undefined;
77 if (view) {
78 // A record naming both spellings (the old one gone, the new one written) is a rename, taken as it is.
79 const named = new Set([...Object.keys(files), ...Object.keys(modes), ...Object.keys(blobs)]);
80 // Both written is two spellings of one new file: refused, naming both.
81 const written = new Map<string, string>();
82 for (const p of named) {
83 if (files[p] === null) continue;
84 const other = written.get(pathKey(p));
85 if (other !== undefined)
86 throw new ProjectError(
87 "bad-request",
88 `${other} and ${p} are one file, spelled two ways (case or Unicode form); record one of them (#49)`,
89 );
90 written.set(pathKey(p), p);
91 }
92 const respell = <T>(o: Readonly<Record<string, T>>): Record<string, T> => {
93 const out: Record<string, T> = {};
94 for (const [path, v] of Object.entries(o)) {
95 const twin = spellingOf(view, path);
96 const existing = named.has(twin) ? path : twin;
97 if (existing !== path && !spelling.some((s) => s.path === path)) spelling.push({ path, existing });
98 out[existing] = v;
99 }
100 return out;
101 };
102 files = respell(files);
103 modes = respell(modes);
104 blobs = respell(blobs);
105 }
106 }
107 if (host.whole) {
108 const known = host.agents.get(agentName);
109 ({ files, blobs } = host.wholeOut(files, blobs, known ? host.agentView(known) : host.sharedView()));
110 }
111 host.checkBlobs(blobs, files);
112 const entries: Entry[] = [];
113 const agent = host.agent(agentName, entries);
114 const touched = host.draftFiles(agent);
115 const changed: string[] = [];
116 // The view before: asked only about the recorded paths, so no other file is laid out (#46).
117 const before = host.agentView(agent);
118 for (const path of [...new Set([...Object.keys(files), ...Object.keys(modes), ...Object.keys(blobs)])].toSorted()) {
119 // A file with a non-canonical path from before #30 can still be removed.
120 const removing =
121 files[path] === null &&
122 modes[path] === undefined &&
123 blobs[path] === undefined &&
124 host.agentView(agent).hasFile(path);
125 if (!removing) checkPath(path);
126 const text = files[path];
127 if (text !== undefined && text !== null && typeof text !== "string")
128 throw new ProjectError("bad-request", `${path}: text or null`);
129 const view = host.agentView(agent);
130 const blob = blobs[path];
131 const target =
132 blob !== undefined ? "" : text === undefined ? undefined : text === null ? null : canonicalText(text);
133 // The session and subagent, when the edit hook sent the file whole (#37: they were dropped, so a hook record read as a command's).
134 if (host.recordFile(agent, view, path, target, time, meta, entries, modes[path], blob)) changed.push(path);
135 }
136 host.overlapNews(agent, touched, entries);
137 host.markedNews(agent, changed, entries);
138 host.newsAboutOwnDraft(agent, entries);
139 const news = host.takeNews(agent, entries);
140 host.flush(entries);
141 const uncomposed = changed.filter((p) => !isComposed(p) && !before.hasFile(p));
142 return {
143 ok: true,
144 changed,
145 news,
146 ...(spelling.length > 0 ? { spelling } : {}),
147 ...(uncomposed.length > 0 ? { uncomposed } : {}),
148 };