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-file.ts?version=309&format=json
122 linesFirst import 52% · codex-capacity-chair 48%Last change codex-capacity-chair in version 175, 3 versions changed it

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

1// Make one file in the agent's view what the folder has. Writes go through
2// RecordFileHost; today the host is Project.
3
4import {
5 type AttrName,
6 type AttrSet,
7 diffChunkedLines,
8 type EditInput,
9 type Hunk,
10 type ReadView,
11 type SpliceEdit,
12 splitLines,
13} from "../../src/engine/index.ts";
14import type { AgentState, Entry, Meta, Mode } from "./project-types.ts";
16/** What recording one file needs from a project. */
17export interface RecordFileHost {
18 addDraft(
19 agent: AgentState,
20 view: ReadView,
21 input: { edits: readonly EditInput[]; attrs?: readonly AttrSet[] },
22 time: number,
23 meta: Meta,
24 entries: Entry[],
25 ): void;
28/**
29 * Make one file in the agent's view what the folder has, as one draft
30 * change: `text` (null: the file is gone; "": an empty file; undefined:
31 * leave its lines alone) and `mode` (only the fields given). Returns
32 * whether anything changed.
33 */
34export function recordFile(
35 host: RecordFileHost,
36 agent: AgentState,
37 view: ReadView,
38 path: string,
39 text: string | null | undefined,
40 time: number,
41 meta: Meta,
42 entries: Entry[],
43 mode: Mode = {},
44 blob?: string,
45 hunks?: readonly Hunk[],
46): boolean {
47 const lines = text === undefined ? undefined : text === null ? "" : text;
48 const edits: SpliceEdit[] =
49 lines === undefined || (hunks !== undefined && sameProjectedText(view, path, lines))
50 ? []
51 : (hunks ?? diffChunkedLines(view.lineChunks(path), splitLines(lines))).map((h) => ({
52 file: path,
53 start: h.start,
54 end: h.end,
55 lines: h.lines,
56 }));
57 return recordFileChanges(
58 host,
59 agent,
60 view,
61 path,
62 text === undefined ? "unchanged" : text === null ? "missing" : text === "" ? "empty" : "present",
63 edits,
64 time,
65 meta,
66 entries,
67 mode,
68 blob,
69 );
72/** Apply a precomputed exact text diff and the normal file attribute rules. */
73export function recordFileChanges(
74 host: RecordFileHost,
75 agent: AgentState,
76 view: ReadView,
77 path: string,
78 state: "unchanged" | "missing" | "empty" | "present",
79 edits: readonly SpliceEdit[],
80 time: number,
81 meta: Meta,
82 entries: Entry[],
83 mode: Mode = {},
84 blob?: string,
85): boolean {
86 const was = view.attrs(path);
87 const exists = view.hasFile(path);
89 const attrs: AttrSet[] = [];
90 const set = (name: AttrName, now: string | null, value: string | null): void => {
91 if (now !== value) attrs.push({ file: path, name, value });
92 };
93 const link = mode.link !== undefined ? mode.link : state === "missing" ? null : was.link;
94 if (state === "missing") {
95 // Gone: no lines, and nothing keeps it.
96 set("exists", was.exists ? "1" : null, null);
97 set("exec", was.exec ? "1" : null, null);
98 } else if (state === "empty" && link === null && blob === undefined) {
99 // An empty file is kept by its exists attribute.
100 set("exists", was.exists ? "1" : null, "1");
101 }
102 // A binary file is its blob and no lines; text, or no file, clears the blob.
103 if (blob !== undefined) set("blob", was.blob, blob);
104 else if (state !== "unchanged") set("blob", was.blob, null);
105 if (mode.exec !== undefined && state !== "missing") set("exec", was.exec ? "1" : null, mode.exec ? "1" : null);
106 set("link", was.link, link);
107 if (edits.length === 0 && attrs.length === 0) return false;
108 if (!exists && state === "missing" && attrs.length === 0) return false;
109 host.addDraft(agent, view, { edits, attrs }, time, meta, entries);
110 return true;
113/** Compare canonical projected text without joining or caching old lines. */
114function sameProjectedText(view: ReadView, path: string, text: string): boolean {
115 let offset = 0;
116 for (const line of view.visibleLines(path)) {
117 if (!text.startsWith(line.text, offset)) return false;
118 offset += line.text.length;
119 if (text[offset++] !== "\n") return false;
120 }
121 return offset === text.length;