{"draft":null,"from":280,"to":281,"files":[{"path":"client/src/join-commands.ts","added":46,"removed":17,"diff":"--- a/client/src/join-commands.ts\n+++ b/client/src/join-commands.ts\n@@ -3,7 +3,7 @@\n import { randomUUID } from \"node:crypto\";\n import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from \"node:fs\";\n import { join, relative, resolve } from \"node:path\";\n-import { Api, ApiError } from \"./api.ts\";\n+import { Api, ApiError, type SyncAnswer } from \"./api.ts\";\n import { bool, type Parsed, str, UsageError } from \"./args.ts\";\n import { writeKnownBlobs } from \"./blobs.ts\";\n import {\n@@ -16,9 +16,9 @@\n \treadBox,\n } from \"./box.ts\";\n import { STARTING_ABSOIGNORE } from \"./check-ignore.ts\";\n-import { CLIENT_DIR } from \"./client-version.ts\";\n+import { builtCopy } from \"./client-version.ts\";\n import { type Env, type Output, plural } from \"./command-env.ts\";\n-import { leftOutText, syncFolder, uploadBlobs } from \"./commands.ts\";\n+import { applyAnswer, leftOutText, pullAnswer, pullWritten, syncFolder, uploadBlobs } from \"./commands.ts\";\n import {\n \tfindRoot,\n \tinstallKey,\n@@ -32,7 +32,7 @@\n import { elsewhereText } from \"./doctor.ts\";\n import { fromEnv } from \"./env-names.ts\";\n import { clearKnown, loadIgnore, readFolder, writeKnown } from \"./folder.ts\";\n-import { modInstalled } from \"./install.ts\";\n+import { installerCommand, modInstalled } from \"./install.ts\";\n import { type FullMode, foldModes, writeKnownModes } from \"./modes.ts\";\n import { initInOrg, siteOf } from \"./orgs.ts\";\n import { projectFileName, readProjectFile, startingProjectFileText, writeStartingProjectFile } from \"./project-file.ts\";\n@@ -141,12 +141,20 @@\n \tsay(`Signing in with ${credential?.words ?? `no credential: nothing here signs in to ${server}`}.\n `);\n \tconst api = new Api({ ...config, ...(token ? { token } : {}) });\n-\tawait uploadBlobs(api, blobs);\n-\tconst answer = await api.init(\n-\t\tObject.fromEntries(files),\n-\t\tmodes,\n-\t\tObject.fromEntries([...blobs].map(([rel, b]) => [rel, b.sha])),\n-\t);\n+\tlet answer: Awaited<ReturnType<Api[\"init\"]>>;\n+\ttry {\n+\t\tawait uploadBlobs(api, blobs);\n+\t\tanswer = await api.init(\n+\t\t\tObject.fromEntries(files),\n+\t\t\tmodes,\n+\t\t\tObject.fromEntries([...blobs].map(([rel, b]) => [rel, b.sha])),\n+\t\t);\n+\t} catch (error) {\n+\t\t// No project was made: the starting files init wrote go too, so the folder is as it was.\n+\t\tif (wroteIgnore) rmSync(absoignore, { force: true });\n+\t\tif (typeof wroteProject !== \"string\") rmSync(join(root, projectFileName(root)), { force: true });\n+\t\tthrow error;\n+\t}\n \twriteConfig(root, config);\n \tclearKnown(root);\n \tfor (const [rel, text] of files) writeKnown(root, rel, text);\n@@ -351,6 +359,16 @@\n \t};\n }\n \n+/** A join that failed before writing files: its state goes, but a key (already here, or made for it) stays for the next try. */\n+function takeBackJoin(root: string): void {\n+\tconst dir = stateDir(root);\n+\tif (!existsSync(join(dir, \"key\"))) {\n+\t\trmSync(dir, { recursive: true, force: true });\n+\t\treturn;\n+\t}\n+\tfor (const entry of readdirSync(dir)) if (entry !== \"key\") rmSync(join(dir, entry), { recursive: true, force: true });\n+}\n+\n export async function runJoin(args: Parsed, env: Env): Promise<Output> {\n \tif (args.positionals.length !== 1) throw new UsageError(\"join takes the project name: abso join <project>\");\n \tconst project = args.positionals[0]!;\n@@ -399,25 +417,34 @@\n \tclearKnown(root);\n \tif (minted) installKey(root, JSON.stringify(minted));\n \t// The token file only with --token-file: readConfig reads it otherwise.\n-\tconst read = readConfig(root, env.env, false);\n+\tlet read: ReturnType<typeof readConfig>;\n+\ttry {\n+\t\tread = readConfig(root, env.env, false);\n+\t} catch (error) {\n+\t\ttakeBackJoin(root);\n+\t\tthrow error;\n+\t}\n \tconst settings = tokenFileAsked ? { ...read, token: credential?.token } : read;\n \t(env.writeErr ?? env.write)(\n \t\t`Signing in with ${minted && box ? `a new key from this box's sign-in, ${box.id}` : hasKey ? \"this folder's key\" : (credential?.words ?? `no credential: nothing here signs in to ${server}`)}.\n `,\n \t);\n \tconst api = new Api(settings);\n-\tlet synced: Awaited<ReturnType<typeof syncFolder>>;\n+\tlet answer: SyncAnswer;\n \ttry {\n \t\t// Before the sync, so the server never takes a record from this folder.\n \t\tif (reader) await api.reader(true);\n \t\t// Every file, even when the server has seen this agent sync before: its plain sync answer would hold only what changed since (#37).\n-\t\tsynced = await syncFolder(root, api, true);\n+\t\tanswer = await pullAnswer(root, api, true);\n \t} catch (error) {\n+\t\t// Nothing is written yet: take back what join wrote so it can run again here.\n+\t\ttakeBackJoin(root);\n \t\tif (!(error instanceof ApiError && error.status === 401 && !box && !settings.token)) throw error;\n-\t\trmSync(stateDir(root), { recursive: true, force: true });\n \t\tconst why = await signedOut(config.server, project);\n \t\tthrow new UsageError(!tokenFileAsked && tokenFile() !== undefined ? `${why} ${TOKEN_FILE_ASKED}` : why);\n \t}\n+\tconst synced = await applyAnswer(root, api, answer);\n+\tpullWritten(root);\n \treturn {\n \t\ttext: [\n \t\t\t`Joined ${project} as ${config.agent} at version ${synced.version}: wrote ${plural(synced.written.length, \"file\")} into ${root}.${reader ? \" It is a reader: status, pull, log, show, files and export work, and nothing it does is recorded or pushed.\" : \"\"}${minted ? ` It signs in with its own key, ${minted.id}, made from this box's sign-in.` : \"\"}`,\n@@ -454,7 +481,9 @@\n }\n \n function hookHint(env: Readonly<Record<string, string | undefined>>): string {\n-\treturn modInstalled(env)\n-\t\t? \"Edits made by Claude Code here are recorded by the abso mod on this machine, signed in as this folder. Anything else: abso record.\"\n-\t\t: `Edits made by Claude Code reach the server through the abso mod, which isn't on this machine yet: node ${join(CLIENT_DIR, \"install-mod.ts\")} installs it, and new chats load it. Anything else: abso record.`;\n+\tif (modInstalled(env))\n+\t\treturn \"Edits made by Claude Code here are recorded by the abso mod on this machine, signed in as this folder. Anything else: abso record.\";\n+\t// A built copy (the npm package) has no installer to point at (abso-core 18).\n+\tif (builtCopy()) return \"abso record sends the edits made here, whoever made them.\";\n+\treturn `Edits made by Claude Code reach the server through the abso mod, which isn't on this machine yet: ${installerCommand(\"install-mod.ts\")} installs it, and new chats load it. Anything else: abso record.`;\n }"}],"unmatched":[]}