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/restore.ts?format=json
107 linesFirst import 88% · codex-capacity-chair 12%Last change codex-capacity-chair in version 163, 3 versions changed it
1// Discard unpublished changes to some files, like git restore. Writes go through RestoreHost;
2// today the host is Project.
3
4import type { AtomId, Graph, Hunk, ReadView } from "../../src/engine/index.ts";
5import { checkReadPath } from "./path-rules.ts";
6import { ProjectError } from "./project-error.ts";
7import type { AgentState, Blobs, Entry, Files, Meta, Mode, Modes, RestoreAnswer } from "./project-types.ts";
8
9/** What restoring unpublished files needs from a project. */
10export interface RestoreHost {
11 readonly graph: Graph;
12 agent(name: string, entries: Entry[]): AgentState;
13 draft(agent: AgentState): AtomId[];
14 touchedFiles(ids: Iterable<AtomId>, views: readonly ReadView[]): Set<string>;
15 apply(entry: Entry): void;
16 view(set: ReadonlySet<AtomId>): ReadView;
17 agentSet(agent: AgentState): Set<AtomId>;
18 agentView(agent: AgentState): ReadView;
19 agentViewWithout?(agent: AgentState, excluded: ReadonlySet<AtomId>): ReadView;
20 recordFile(
21 agent: AgentState,
22 view: ReadView,
23 path: string,
24 text: string | null | undefined,
25 time: number,
26 meta: Meta,
27 entries: Entry[],
28 mode?: Mode,
29 blob?: string,
30 hunks?: readonly Hunk[],
31 ): boolean;
32 flush(entries: Entry[]): void;
35/**
36 * Discard the agent's unpublished changes to some files (all of them with
37 * `files` null), like git restore. Changes that touch only those files and
38 * that no kept change builds on leave the draft; anything left over is
39 * undone by a new change that puts the files back. Answers with each
40 * file's text now, for the folder.
41 */
42export function restore(
43 host: RestoreHost,
44 agentName: string,
45 files: readonly string[] | null,
46 time: number,
47): RestoreAnswer {
48 const entries: Entry[] = [];
49 const agent = host.agent(agentName, entries);
50 const draft = host.draft(agent);
51 const touched = host.touchedFiles(draft, [host.agentView(agent)]);
52 if (files !== null && !Array.isArray(files))
53 throw new ProjectError("bad-request", "restore needs {files: [path]} or {all: true}");
54 for (const path of files ?? []) checkReadPath(path);
55 const targets = new Set(files ?? touched);
56 // Drop what touches only the targets, unless a change that stays builds on it.
57 const drop = new Set(draft.filter((id) => host.graph.filesOf(id).every((f) => targets.has(f))));
58 const kept = draft.filter((id) => !drop.has(id));
59 for (const id of host.graph.closureOf(kept.length > 0 ? kept : [])) drop.delete(id);
60 if (drop.size > 0) {
61 const entry: Entry = { type: "discard", agent: agent.name, atoms: draft.filter((id) => drop.has(id)) };
62 entries.push(entry);
63 host.apply(entry);
64 }
65 // Whatever is still changed in a target goes back with a new change.
66 const now = host.draft(agent);
67 const without = new Set(now);
68 const originalAgent = { ...agent, mine: [...agent.mine] };
69 const originalView = () =>
70 host.agentViewWithout
71 ? host.agentViewWithout(originalAgent, without)
72 : host.view(new Set([...host.agentSet(originalAgent)].filter((id) => !without.has(id))));
73 for (const path of [...targets].toSorted()) {
74 if (!touched.has(path)) continue;
75 // A previous file may have mutated the graph. Reopen the same base view.
76 const original = originalView();
77 const was = original.attrs(path);
78 host.recordFile(
79 agent,
80 host.agentView(agent),
81 path,
82 original.hasFile(path) ? original.text(path) : null,
83 time,
84 {},
85 entries,
86 { exec: was.exec, link: was.link },
87 was.blob ?? undefined,
88 );
89 }
90 const view = host.agentView(agent);
91 const out: Files = {};
92 const modes: Modes = {};
93 const blobs: Blobs = {};
94 for (const path of [...targets].filter((p) => touched.has(p)).toSorted()) {
95 const a = view.attrs(path);
96 if (a.blob !== null) blobs[path] = a.blob;
97 else out[path] = view.hasFile(path) ? view.text(path) : null;
98 if (a.exec || a.link !== null) modes[path] = { exec: a.exec, link: a.link };
99 }
100 host.flush(entries);
101 return {
102 files: out,
103 dropped: drop.size,
104 ...(Object.keys(modes).length > 0 ? { modes } : {}),
105 ...(Object.keys(blobs).length > 0 ? { blobs } : {}),
106 };