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/client/src/drafts.ts?version=155&format=json
168 linesFirst import 97% · rename 3%Last change rename in version 100, 3 versions changed it

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

1// abso switch, land and draft-sync: named drafts (#10, #22).
2//
3// Each agent has its own unpublished draft in each space (shared, or a named
4// draft). Switching parks the draft on the server in the space it leaves and
5// brings back what was parked in the space it moves to, so nothing needs
6// publishing first. `switch -c <name>` instead takes the current unpublished
7// changes into a new named draft.
8
9import { readFileSync } from "node:fs";
10import { bool, type Parsed, str, UsageError } from "./args.ts";
11import {
12 applyAnswer,
13 changedFilesText,
14 collisionsHeading,
15 collisionText,
16 type Env,
17 EXIT,
18 mergedLinesText,
19 type Output,
20 open,
21 plural,
22 recordedFirst,
23 recordFolder,
24} from "./commands.ts";
25import { isLocalOnly } from "./core.ts";
26import { absolute } from "./folder.ts";
27import { refusedText } from "./safety.ts";
29/** The name that means shared, not a named draft. */
30const SHARED = "shared";
32function draftName(raw: string): string {
33 if (!/^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(raw))
34 throw new UsageError(`draft names are letters, digits, dot, dash and underscore (got ${JSON.stringify(raw)})`);
35 return raw;
38export async function runSwitch(args: Parsed, env: Env): Promise<Output> {
39 if (args.positionals.length !== 1)
40 throw new UsageError(
41 "switch takes one name: abso switch <draft>, abso switch -c <new draft>, or abso switch shared",
42 );
43 const raw = args.positionals[0]!;
44 const carry = bool(args, "carry");
45 const name = raw === SHARED ? null : draftName(raw);
46 if (carry && name === null)
47 throw new UsageError("-c starts a new named draft; shared already exists (use: abso switch shared)");
48 const { root, api } = open(env);
49 // Folder-only edits must be on the server first, or the new view would overwrite them instead of parking them.
50 const recorded = await recordFolder(root, api);
51 // What the folder held before, for each file the switch or the sync touches, so only the net change is reported.
52 const before = new Map<string, string | null>();
53 const note = (files: Readonly<Record<string, unknown>>): void => {
54 for (const rel of Object.keys(files)) if (!before.has(rel)) before.set(rel, onDisk(root, rel));
55 };
56 const answer = await api.switchDraft(name, carry);
57 note(answer.files);
58 const switched = await applyAnswer(root, api, answer);
59 // The switch brings back the space as it was when parked; sync straight away so the folder shows it as it is now (#22).
60 const syncAnswer = await api.sync();
61 note(syncAnswer.files);
62 const synced = await applyAnswer(root, api, syncAnswer);
63 const touched = new Set([...switched.written, ...switched.deleted, ...synced.written, ...synced.deleted]);
64 const written: string[] = [];
65 const deleted: string[] = [];
66 for (const rel of [...touched].sort()) {
67 const now = onDisk(root, rel);
68 if (now === before.get(rel)) continue;
69 (now === null ? deleted : written).push(rel);
70 }
71 const where = answer.draft === null ? "shared" : `draft ${answer.draft}`;
72 const kept = [...new Set([...switched.kept, ...synced.kept])];
73 const dropped = [...new Set([...switched.dropped, ...synced.dropped])];
74 const collisions = [...switched.collisions, ...synced.collisions];
75 const lines = [
76 ...recordedFirst(recorded),
77 carry
78 ? `Started ${where}, carrying your unpushed changes into it.`
79 : `Switched to ${where}. Your unpushed changes where you were are parked there, and come back when you switch back.`,
80 `It is at version ${synced.version}. ${written.length + deleted.length === 0 ? "No files changed in this folder." : `${plural(written.length, "file")} updated, ${deleted.length} deleted:`}`,
81 ...changedFilesText("switch", written, deleted, [], bool(args, "verbose")),
82 ...refusedText([...switched.refused, ...synced.refused]),
83 ...mergedLinesText([...switched.merged, ...synced.merged]),
84 ...(kept.length > 0 ? [`Left alone because they changed while switching (run abso pull): ${kept.join(", ")}`] : []),
85 ...(dropped.length > 0 ? [`Dropped from your draft, kept on disk (these stay local): ${dropped.join(", ")}`] : []),
86 ...(collisions.length > 0 ? [collisionsHeading(collisions), ...collisions.map(collisionText)] : []),
87 ...(answer.draft !== null
88 ? ['Push into this draft with abso push; land it in shared with abso land -m "why".']
89 : []),
90 ];
91 return {
92 text: lines.join("\n"),
93 json: {
94 draft: answer.draft,
95 version: synced.version,
96 carried: carry,
97 recorded: recorded.sent,
98 written,
99 deleted,
100 kept,
101 dropped,
102 collisions,
103 },
104 };
107/** A file's bytes as text, or null when there is no file. */
108function onDisk(root: string, rel: string): string | null {
109 try {
110 return readFileSync(absolute(root, rel), "latin1");
111 } catch {
112 return null;
113 }
116export async function runLand(args: Parsed, env: Env): Promise<Output> {
117 const message = str(args, "message");
118 if (!message) throw new UsageError('say why with -m "<message>"');
119 const { root, api } = open(env);
120 const recorded = await recordFolder(root, api);
121 const answer = await api.land(message);
122 const pre = recordedFirst(recorded);
123 if (answer.ok) {
124 const what =
125 answer.changes === 0
126 ? `Nothing to land: the draft has nothing shared doesn't. Shared is at version ${answer.version}.`
127 : `Landed in shared as version ${answer.version}.`;
128 return {
129 text: [...pre, what].join("\n"),
130 json: { version: answer.version, changes: answer.changes ?? null, recorded: recorded.sent },
131 };
132 }
133 const collisions = (answer.collisions ?? []).filter((c) => !isLocalOnly(c.file));
134 return {
135 exit: EXIT.refused,
136 text: [
137 ...pre,
138 `Not landed: ${plural(collisions.length, "collision")} with work that landed in shared since the draft's base.`,
139 ...collisions.map(collisionText),
140 "Run abso draft-pull to move the draft onto shared, settle those lines in the draft, push, then land again.",
141 ].join("\n"),
142 json: { collisions, recorded: recorded.sent },
143 };
146export async function runDraftSync(_args: Parsed, env: Env): Promise<Output> {
147 const { root, api } = open(env);
148 const moved = await api.draftSync();
149 // Bring the moved draft's new view into this folder straight away.
150 const recorded = await recordFolder(root, api);
151 const applied = await applyAnswer(root, api, await api.sync());
152 const lines = [
153 `Moved draft ${moved.draft} onto shared version ${moved.sharedBase} (the draft is at version ${moved.version}).`,
154 ...recordedFirst(recorded),
155 applied.written.length + applied.deleted.length === 0
156 ? "No files changed in this folder."
157 : `${plural(applied.written.length, "file")} updated, ${applied.deleted.length} deleted.`,
158 ...refusedText(applied.refused),
159 ...mergedLinesText(applied.merged),
160 ...(applied.collisions.length > 0
161 ? [
162 `${plural(applied.collisions.length, "collision")} to settle in the draft before it lands:`,
163 ...applied.collisions.map(collisionText),
164 ]
165 : []),
166 ];
167 return { text: lines.join("\n"), json: { ...applied, ...moved } };