- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/edit.ts?format=json
162 linesFirst import 96% · codex-capacity-chair 4%Last change codex-capacity-chair in version 143, 2 versions changed it
1// An Edit, Write or MultiEdit from the hook. Writes go through EditHost; today
2// the host is Project.
4import {
5 canonicalText,
6 type EditInput,
7 EngineError,
8 type Hunk,
9 normalizeText,
10 type ReadView,
11} from "../../src/engine/index.ts";
12import { checkPath } from "./path-rules.ts";
13import { isComposed } from "./pathkey.ts";
14import { ProjectError } from "./project-error.ts";
15import { replaceOnce } from "./project-helpers.ts";
16import type {
17 AgentState,
18 EditAnswer,
19 EditBody,
20 Entry,
21 Files,
22 Meta,
23 Mode,
24 Modes,
25 ProjectOptions,
26 RecordAnswer,
27} from "./project-types.ts";
28import { spellingOf } from "./spelling.ts";
30/** What an edit needs from a project. */
31export interface EditHost {
32 readonly agents: ReadonlyMap<string, AgentState>;
33 readonly whole: ProjectOptions["whole"];
34 initialized(): boolean;
35 agentView(agent: AgentState): ReadView;
36 sharedView(): ReadView;
37 isWhole(path: string, length: number, view: ReadView | undefined, rules: readonly string[]): boolean;
38 wholeRules(config?: string): string[];
39 record(
40 agentName: string,
41 files: Files,
42 time: number,
43 modes?: Modes,
44 blobs?: Readonly<Record<string, string>>,
45 meta?: Meta,
46 ): RecordAnswer;
47 agent(name: string, entries: Entry[]): AgentState;
48 draftFiles(agent: AgentState): Set<string>;
49 recordFile(
50 agent: AgentState,
51 view: ReadView,
52 path: string,
53 text: string | null | undefined,
54 time: number,
55 meta: Meta,
56 entries: Entry[],
57 mode?: Mode,
58 blob?: string,
59 hunks?: readonly Hunk[],
60 ): boolean;
61 addDraft(
62 agent: AgentState,
63 view: ReadView,
64 input: { edits: readonly EditInput[] },
65 time: number,
66 meta: Meta,
67 entries: Entry[],
68 ): void;
69 overlapNews(agent: AgentState, before: ReadonlySet<string>, entries: Entry[]): void;
70 markedNews(agent: AgentState, files: readonly string[], entries: Entry[]): void;
71 newsAboutOwnDraft(agent: AgentState, entries: Entry[]): void;
72 takeNews(agent: AgentState, entries: Entry[]): string[];
73 flush(entries: Entry[]): void;
74}
76/** An Edit, Write or MultiEdit from the hook, against the agent's view. */
77export function edit(host: EditHost, agentName: string, body: EditBody, time: number): EditAnswer {
78 if (typeof body !== "object" || body === null || typeof body.file !== "string") {
79 throw new ProjectError("bad-request", "edit needs a file");
80 }
81 // A new name not in composed form gets a gentle warning (#49).
82 const known = host.agents.get(agentName);
83 const view = known ? host.agentView(known) : host.initialized() ? host.sharedView() : undefined;
84 const fresh =
85 !isComposed(body.file) && !!view && spellingOf(view, body.file) === body.file && !view.hasFile(body.file);
86 const answer = editInner(host, agentName, body, time);
87 return fresh && answer.ok ? { ...answer, uncomposed: [body.file] } : answer;
88}
90function outOfStep(host: EditHost, entries: Entry[]): EditAnswer {
91 host.flush(entries);
92 return { ok: false, reason: "out of step" };
93}
95function editInner(host: EditHost, agentName: string, body: EditBody, time: number): EditAnswer {
96 checkPath(body.file);
97 {
98 // One file whatever the case of its spelling (#49).
99 const known = host.agents.get(agentName);
100 const view = known ? host.agentView(known) : host.initialized() ? host.sharedView() : undefined;
101 const existing = view ? spellingOf(view, body.file) : body.file;
102 if (existing !== body.file) {
103 const answer = edit(host, agentName, { ...body, file: existing }, time);
104 return answer.ok ? { ...answer, spelling: [{ path: body.file, existing }] } : answer;
105 }
106 }
107 if (host.whole) {
108 // A whole file has no lines to edit (#46): a Write records it; an Edit is out of step, so the client sends the file.
109 const known = host.agents.get(agentName);
110 const content = "content" in body && typeof body.content === "string" ? body.content : undefined;
111 if (
112 host.isWhole(
113 body.file,
114 content?.length ?? 0,
115 known ? host.agentView(known) : host.sharedView(),
116 host.wholeRules(),
117 )
118 ) {
119 if (content === undefined) return { ok: false, reason: "out of step" };
120 const meta = { session: body.session, subagent: body.subagent, client: body.client };
121 return { ok: true, news: host.record(agentName, { [body.file]: content }, time, {}, {}, meta).news };
122 }
123 }
124 const entries: Entry[] = [];
125 const agent = host.agent(agentName, entries);
126 const touched = host.draftFiles(agent);
127 const view = host.agentView(agent);
128 const current = view.text(body.file);
129 const meta = { session: body.session, subagent: body.subagent, client: body.client };
131 if ("content" in body) {
132 if (typeof body.content !== "string") throw new ProjectError("bad-request", "content must be a string");
133 host.recordFile(agent, view, body.file, canonicalText(body.content), time, meta, entries);
134 } else if ("edits" in body) {
135 if (!Array.isArray(body.edits)) throw new ProjectError("bad-request", "edits must be a list");
136 let text = current;
137 for (const e of body.edits) {
138 const next = replaceOnce(text, e?.old, e?.new);
139 if (next === undefined) return outOfStep(host, entries);
140 text = next;
141 }
142 host.recordFile(agent, view, body.file, canonicalText(text), time, meta, entries);
143 } else {
144 if (typeof body.old !== "string" || typeof body.new !== "string") {
145 throw new ProjectError("bad-request", "an edit needs old and new strings");
146 }
147 if (replaceOnce(current, body.old, body.new) === undefined) return outOfStep(host, entries);
148 if (normalizeText(body.old) !== normalizeText(body.new)) {
149 try {
150 host.addDraft(agent, view, { edits: [{ file: body.file, old: body.old, new: body.new }] }, time, meta, entries);
151 } catch (err) {
152 if (!(err instanceof EngineError) || err.code !== "empty-atom") throw err;
153 }
154 }
155 }
156 host.overlapNews(agent, touched, entries);
157 host.markedNews(agent, [body.file], entries);
158 host.newsAboutOwnDraft(agent, entries);
159 const news = host.takeNews(agent, entries);
160 host.flush(entries);
161 return { ok: true, news };
162}