{"draft":null,"from":184,"to":185,"files":[{"path":"client/src/join-commands.ts","added":121,"removed":3,"diff":"--- a/client/src/join-commands.ts\n+++ b/client/src/join-commands.ts\n@@ -1,11 +1,20 @@\n // abso init and abso join: start a project from a folder, or join one into a folder.\n \n-import { existsSync, mkdirSync, readdirSync, rmSync, statSync, writeFileSync } from \"node:fs\";\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 { bool, type Parsed, str, UsageError } from \"./args.ts\";\n import { writeKnownBlobs } from \"./blobs.ts\";\n-import { defaultAgentName, mintFreeName, mintKey, readBox } from \"./box.ts\";\n+import {\n+\ttype BoxRecord,\n+\tboxCreateProject,\n+\tdefaultAgentName,\n+\tmintFreeName,\n+\tmintKey,\n+\ttype NewProject,\n+\treadBox,\n+} from \"./box.ts\";\n import { STARTING_ABSOIGNORE } from \"./check-ignore.ts\";\n import { CLIENT_DIR } from \"./client-version.ts\";\n import { type Env, type Output, plural } from \"./command-env.ts\";\n@@ -16,7 +25,7 @@\n import { clearKnown, loadIgnore, readFolder, writeKnown } from \"./folder.ts\";\n import { modInstalled } from \"./install.ts\";\n import { type FullMode, foldModes, writeKnownModes } from \"./modes.ts\";\n-import { siteOf } from \"./orgs.ts\";\n+import { initInOrg, siteOf } from \"./orgs.ts\";\n import { projectFileName, readProjectFile, startingProjectFileText, writeStartingProjectFile } from \"./project-file.ts\";\n import { refusedText } from \"./safety.ts\";\n import { chooseServer, serverFlag } from \"./server-choice.ts\";\n@@ -48,6 +57,15 @@\n \tif (findRoot(root) === root) throw new UsageError(`${root} already has .abso/config.json`);\n \tconst project = str(args, \"project\");\n \tif (!project) throw new UsageError(\"give the project a name with --project <name>\");\n+\tif (bool(args, \"public\") && bool(args, \"private\")) throw new UsageError(\"--public or --private, not both\");\n+\t// A box signed in here, and no token for this server: the project is made as the person who approved the box,\n+\t// the way the website makes one (abso 663).\n+\tconst server = serverFrom(args, env);\n+\tconst box = readBox(server);\n+\tif (box && !tokenFor(server, env.env) && !fromEnv(env.env, \"KEY_FILE\"))\n+\t\treturn initFromBox(args, env, root, project, server, box);\n+\tconst org = str(args, \"org\");\n+\tif (org !== undefined) return initInOrg(org, server);\n \tconst config = makeSettings(args, env, project);\n \tconst asIs = bool(args, \"as-is\");\n \tconst absoignore = join(root, \".absoignore\");\n@@ -99,7 +117,107 @@\n \t};\n }\n \n+/** The most a new project's folder may be from a box: the server takes a body up to 25 MB, base64 and JSON included. */\n+const BOX_INIT_MAX = 24 * 1024 * 1024;\n+/** The Idempotency-Key of an abso init that didn't finish, so running it again makes the same project (abso 663). */\n+const INIT_KEY = \"init-key\";\n+\n /**\n+ * abso init from a signed-in box (abso 663): the server makes the project as the person who approved the box, the way the\n+ * website's New project does (owner, visibility, address), with the folder as its first version. This folder then\n+ * joins it like abso join, with its own key from the box, so it works at once with no new approval.\n+ */\n+async function initFromBox(\n+\targs: Parsed,\n+\tenv: Env,\n+\troot: string,\n+\tproject: string,\n+\tserver: string,\n+\tbox: BoxRecord,\n+): Promise<Output> {\n+\tif (!NAME.test(project))\n+\t\tthrow new UsageError(\n+\t\t\t`project names are letters, digits, dot, dash and underscore (got ${JSON.stringify(project)})`,\n+\t\t);\n+\tconst named = str(args, \"agent\");\n+\tif (named !== undefined) agentFrom(args);\n+\tconst asIs = bool(args, \"as-is\");\n+\tconst absoignore = join(root, \".absoignore\");\n+\tconst wroteIgnore = !asIs && !existsSync(absoignore) && !existsSync(join(root, \".avcsignore\"));\n+\tif (wroteIgnore) writeFileSync(absoignore, STARTING_ABSOIGNORE);\n+\tconst wroteProject = asIs ? \"exists\" : writeStartingProjectFile(root);\n+\tconst { files, blobs, leftOut } = readFolder(root, loadIgnore(root));\n+\tconst listed: NonNullable<NewProject[\"files\"]>[number][] = [\n+\t\t...[...files].map(([path, text]) => ({ path, text })),\n+\t\t...[...blobs].map(([path, b]) => ({ path, base64: Buffer.from(b.bytes).toString(\"base64\") })),\n+\t];\n+\tconst org = str(args, \"org\");\n+\tconst body: NewProject = {\n+\t\tname: project,\n+\t\t...(org === undefined ? {} : { owner: org }),\n+\t\t...(bool(args, \"public\")\n+\t\t\t? { visibility: \"public\" as const }\n+\t\t\t: bool(args, \"private\")\n+\t\t\t\t? { visibility: \"private\" as const }\n+\t\t\t\t: {}),\n+\t\t...(listed.length > 0 ? { files: listed } : {}),\n+\t};\n+\tif (JSON.stringify(body).length > BOX_INIT_MAX)\n+\t\tthrow new UsageError(\n+\t\t\t`${root} is over 24 MB to send. Make ${project} with abso init on a smaller folder (or on the website), then copy the rest in and push.`,\n+\t\t);\n+\t// One key per folder until it finishes: running the same abso init again is the same project, never a second.\n+\tconst keyPath = join(stateDir(root), INIT_KEY);\n+\tlet key = existsSync(keyPath) ? readFileSync(keyPath, \"utf8\").trim() : \"\";\n+\tif (!/^[0-9a-f-]{36}$/.test(key)) {\n+\t\tkey = randomUUID();\n+\t\tmkdirSync(stateDir(root), { recursive: true });\n+\t\twriteFileSync(keyPath, key);\n+\t}\n+\tconst made = await boxCreateProject(box, body, key);\n+\tconst visibility = typeof made.visibility === \"string\" ? made.visibility : \"private\";\n+\tconst minted =\n+\t\tnamed !== undefined\n+\t\t\t? { ...(await mintKey(box, project, named)), agent: named }\n+\t\t\t: await mintFreeName(box, project, defaultAgentName());\n+\tconst config = { server, project, agent: minted.agent };\n+\twriteConfig(root, config);\n+\tclearKnown(root);\n+\tinstallKey(root, JSON.stringify({ key: minted.key, id: minted.id }));\n+\trmSync(keyPath, { force: true });\n+\tconst synced = await syncFolder(root, new Api(readConfig(root, env.env)), true);\n+\tconst owner = org ?? \"you\";\n+\treturn {\n+\t\ttext: [\n+\t\t\t`Created project ${project} (${visibility}, owned by ${owner}) at version ${synced.version} from ${plural(listed.length, \"file\")} in ${root}.`,\n+\t\t\t...leftOutText(leftOut),\n+\t\t\t...(wroteIgnore\n+\t\t\t\t? [\"Wrote a starting .absoignore (Claude Code state and operating system files); edit it like .gitignore.\"]\n+\t\t\t\t: []),\n+\t\t\t...startingProjectFileText(wroteProject),\n+\t\t\t`This folder is agent ${minted.agent}, with its own key, ${minted.id}, made from this box's sign-in. Settings are in .abso/config.json.`,\n+\t\t\t...refusedText(synced.refused),\n+\t\t\t...(synced.written.length > 0 ? [`Wrote ${plural(synced.written.length, \"file\")} from shared.`] : []),\n+\t\t\thookHint(env.env),\n+\t\t].join(\"\\n\"),\n+\t\tjson: {\n+\t\t\tproject,\n+\t\t\tid: made.id ?? null,\n+\t\t\towner: made.owner ?? null,\n+\t\t\tvisibility,\n+\t\t\tversion: synced.version,\n+\t\t\tfiles: listed.length,\n+\t\t\troot,\n+\t\t\tagent: minted.agent,\n+\t\t\tkey_id: minted.id,\n+\t\t\twroteAbsoignore: wroteIgnore,\n+\t\t\twroteProjectFile: typeof wroteProject === \"string\" ? null : wroteProject,\n+\t\t\tleftOut,\n+\t\t},\n+\t};\n+}\n+\n+/**\n  * Where abso join writes: --dir, else here when --agent is given (as before, so a folder prepared with\n  * abso key install --server is joined where its key is). With no --agent: here when this folder is empty, else\n  * ./<project>, which has to be new or empty so nobody's files are written over. A folder holding only abso's own"}],"unmatched":[]}