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=36&format=json
169 linesFirst import 100%Last change First import in version 0, 1 version changed it

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