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

Versions 175 to 318

Compare

+127 −9 in 10 places
Lines 21–35 at 318 (were 21–28 at 175)
2121 */
2222export interface Grant {
2323 readonly projects: readonly { readonly project: string; readonly role: string }[];
24+ /** Version 2 grants (abso 663): member-all is every project of the approver's, now and later, at most this role. */
25+ readonly scopes?: readonly { readonly kind: string; readonly role: string }[];
2426}
2527
28+/** The role a box has in all its approver's projects, now and later, or null when its grant is a list (abso 663). */
29+export function allProjectsRole(grant: Grant): string | null {
30+ return grant.scopes?.find((scope) => scope.kind === "member-all")?.role ?? null;
31+}
2633/** Requested access is a suggestion for the person approving, never an authorization. */
2734export interface LoginRequest {
2835 readonly projects?: readonly { readonly project: string; readonly role: "reader" | "writer" | "owner" }[];
Lines 161–167 at 318 (were 154–159 at 175)
154161 body: unknown,
155162 credential: string | undefined,
156163 fetcher: typeof fetch,
164+ headers: Readonly<Record<string, string>> = {},
157165): Promise<Answer> {
158166 const secrets = credential ? [credential] : [];
159167 let response: Response;
Lines 170–176 at 318 (were 162–167 at 175)
162170 method: "POST",
163171 headers: {
164172 "content-type": "application/json",
173+ ...headers,
165174 ...(credential ? { authorization: `Box ${credential}` } : {}),
166175 },
167176 body: JSON.stringify(body),
Lines 222–228 at 318 (were 213–219 at 175)
213222}
214223
215224/** This machine's name, as the server takes a box's name: 1-64 letters, digits, '.', '_' or '-'. */
216−function boxName(): string {
225+export function boxName(): string {
217226 const name = hostname()
218227 .replace(/[^A-Za-z0-9._-]/g, "-")
219228 .replace(/^[^A-Za-z0-9]+/, "")
Lines 254–282 at 318 (were 245–252 at 175)
245254 readonly say: (line: string) => void;
246255 /** Opens the approve page in a browser, answering whether it tried. None where it shouldn't (--no-browser, SSH, CI). */
247256 readonly open?: (url: string) => boolean;
257+ /**
258+ * abso 684: in a joined folder, asks a person in its project to approve this sign-in, through an abso ask, so nobody
259+ * relays the code. Answers who was asked, or null to fall back to the code alone.
260+ */
261+ readonly ask?: (codes: { readonly device: string; readonly user: string }) => Promise<LoginAsked | null>;
248262}
249263
264+/** A sign-in asked of people (abso 684). */
265+export interface LoginAsked {
266+ readonly project: string;
267+ /** "the person who approved this agent's box", or "the owners of <project>". */
268+ readonly who: string;
269+ readonly asks: readonly number[];
270+ /** Milliseconds: when the asked sign-in runs out. */
271+ readonly expiresAt: number;
272+ /** The most the box may be granted: the asking agent's own role. */
273+ readonly role: string;
274+ /** Withdraws the asks once nobody answered in time. */
275+ readonly withdraw: () => Promise<void>;
276+}
278+const CENTRAL = new Intl.DateTimeFormat("en-US", { timeZone: "America/Chicago", hour: "numeric", minute: "2-digit" });
250280/**
251281 * Asks `server` for a code, shows it with its link, and waits for a person to
252282 * approve it. Answers the box's record, already written to the config folder.
Lines 299–335 at 318 (were 269–284 at 175)
269299 if (typeof code !== "string" || typeof link !== "string" || typeof device !== "string")
270300 throw new UsageError(`${originOf(server)} didn't give a code to approve`);
271301 // The page opens with the code in it, so nobody copies a link by hand. The link and code are printed all the same.
272− if (io.open?.(link)) io.say("Opening the approve page in your browser.");
273− io.say(`To sign this box in, open ${link} and approve the code ${code}.`);
274− io.say("Waiting for approval. Ctrl-C stops waiting, and abso stops by itself when the code runs out.");
275− const until = now() + expiresIn * 1000;
302+ // A failed ask falls back to the code, and says why, so a person can still approve it (abso 684).
303+ const asked = io.ask
304+ ? await io.ask({ device, user: code }).catch((e: unknown) => {
305+ io.say(
306+ `Couldn't ask a person to approve this (${e instanceof Error ? e.message : String(e)}), so here is the code.`,
307+ );
308+ return null;
309+ })
310+ : null;
311+ if (asked) {
312+ const askWord = asked.asks.length === 1 ? `ask ${asked.asks[0]}` : `asks ${asked.asks.join(", ")}`;
313+ io.say(`Asked ${asked.who} (${askWord} in ${asked.project}) to sign this box in, as at most ${asked.role}.`);
314+ io.say(
315+ `Waiting for their answer, until ${CENTRAL.format(new Date(asked.expiresAt))} CT. To approve it yourself instead: ${link}`,
316+ );
317+ } else {
318+ if (io.open?.(link)) io.say("Opening the approve page in your browser.");
319+ io.say(`To sign this box in, open ${link} and approve the code ${code}.`);
320+ io.say("Waiting for approval. Ctrl-C stops waiting, and abso stops by itself when the code runs out.");
321+ }
322+ const until = asked ? Math.min(asked.expiresAt, now() + MAX_LOGIN_WAIT_S * 1000) : now() + expiresIn * 1000;
276323 let toldAt = now();
277324 while (now() < until) {
278325 await sleep(interval * 1000);
279326 // A wait with no word looks like a hang, so say it's still waiting now and then.
280327 if (now() - toldAt >= STILL_WAITING_MS) {
281− io.say(`Still waiting for the code ${code} to be approved.`);
328+ io.say(
329+ asked
330+ ? `Still waiting (${asked.who} hasn't answered yet).`
331+ : `Still waiting for the code ${code} to be approved.`,
332+ );
282333 toldAt = now();
283334 }
284335 const a = await call(server, "/login/token", { device_code: device }, undefined, fetcher);
Lines 339–352 at 318 (were 288–295 at 175)
288339 interval += 5;
289340 continue;
290341 }
291− if (a.body.error === "access_denied") throw new UsageError(`the code ${code} was turned down.`);
292− throw new UsageError(`the code ${code} ran out. Run abso login again.`);
342+ if (a.body.error === "access_denied")
343+ throw new UsageError(asked ? `${asked.who} turned this sign-in down.` : `the code ${code} was turned down.`);
344+ if (asked) await asked.withdraw().catch(() => undefined);
345+ throw new UsageError(
346+ asked
347+ ? "nobody answered in time, so nothing was granted. Run abso login again."
348+ : `the code ${code} ran out. Run abso login again.`,
349+ );
293350 }
294351 const { box_token: credential, box_id: id, grant, expires_at } = a.body;
295352 const expiresAt = typeof expires_at === "string" ? Date.parse(expires_at) : Number.NaN;
Lines 356–365 at 318 (were 299–304 at 175)
299356 writeBox(record, home);
300357 return record;
301358 }
359+ if (asked) {
360+ await asked.withdraw().catch(() => undefined);
361+ throw new UsageError("nobody answered in time, so nothing was granted. Run abso login again.");
362+ }
302363 throw new UsageError(`nobody approved the code ${code} in time. Run abso login again.`);
303364}
304365
Lines 377–436 at 318 (were 316–321 at 175)
316377 return { key, id };
317378}
318379
380+/** What a new project from this box starts with (abso 663): the website's own create, as the person who approved the box. */
381+export interface NewProject {
382+ readonly name: string;
383+ readonly owner?: string;
384+ readonly visibility?: "public" | "private";
385+ readonly files?: readonly (
386+ | { readonly path: string; readonly text: string }
387+ | { readonly path: string; readonly base64: string }
388+ )[];
389+}
391+/**
392+ * Makes a project as the person who approved this box (abso init, abso 663). One key for every try, so a retry after
393+ * a lost answer or a pending one is the same project, never a second.
394+ */
395+export async function boxCreateProject(
396+ record: BoxRecord,
397+ project: NewProject,
398+ key: string,
399+ fetcher: typeof fetch = fetch,
400+ sleep: (ms: number) => Promise<void> = (ms) => new Promise((done) => setTimeout(done, ms)),
401+): Promise<Record<string, unknown>> {
402+ for (let attempt = 1; ; attempt++) {
403+ const a = await call(record.server, "/box/projects", project, record.credential, fetcher, {
404+ "Idempotency-Key": key,
405+ });
406+ const pending = (a.body.operation as { state?: unknown } | undefined)?.state === "pending";
407+ if (!pending) return a.body;
408+ if (attempt >= 10)
409+ throw new UsageError(
410+ `${originOf(record.server)} is still making ${project.name}. Run the same abso init again in a minute: it picks up where this left off.`,
411+ );
412+ await sleep(1000 * attempt);
413+ }
414+}
416+/** Makes an organization as the person who approved this box (abso org new, abso 663). */
417+export async function boxCreateOrg(
418+ record: BoxRecord,
419+ name: string,
420+ displayName: string | undefined,
421+ fetcher: typeof fetch = fetch,
422+): Promise<Record<string, unknown>> {
423+ return (
424+ await call(
425+ record.server,
426+ "/box/orgs",
427+ { name, ...(displayName === undefined ? {} : { displayName }) },
428+ record.credential,
429+ fetcher,
430+ )
431+ ).body;
432+}
319434/** The agent name abso join takes when none is given: the box's name, lower-cased. */
320435export function defaultAgentName(): string {
321436 return boxName().toLowerCase();
Lines 490–501 at 318 (were 375–383 at 175)
375490 if (!record) return `This box isn't signed in to ${originOf(server)}. abso login signs it in.`;
376491 const g = record.grant;
377492 const expired = record.expiresAt <= now;
493+ const all = allProjectsRole(g);
378494 return [
379495 `Signed in to ${record.server} as box ${record.id}${expired ? ", but the sign-in has expired. abso login renews it." : "."}`,
380− `Projects: ${g.projects.map((p) => `${p.project} (${p.role})`).join(", ")}.`,
496+ all === null
497+ ? `Projects: ${g.projects.map((p) => `${p.project} (${p.role})`).join(", ")}.`
498+ : `Projects: all of yours, now and later (${all} at most). abso init makes a new one; abso org new makes an organization.`,
381499 `${expired ? "Expired" : "Expires"} ${day(record.expiresAt)} UTC.`,
382500 ].join("\n");
383501}
MCP
compare on this site's /mcp
CLI
abso diff 175 318 client/src/box.ts --json
JSON
/christopher/abso-core/compare/175..318?file=client%2Fsrc%2Fbox.ts&format=json