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/worker/project/project-helpers.ts?format=json
147 linesFirst import 82% · engine 15% · whytrail 3% · infra-split 1%Last change whytrail in version 193, 4 versions changed it
1import { normalizeText } from "../../src/engine/index.ts";
2import type { Pair } from "./collisions.ts";
3import { ProjectError } from "./project-error.ts";
4import type { PersonSender, Refusal, RefusalCounts } from "./project-types.ts";
5import type { Published } from "./spaces.ts";
6
7/** A draft's or an agent's name: letters, digits, '.', '_' and '-', up to 64. */
8export const NAME = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/;
9
10/** A person's id and name as the Worker passed them (#41): 1-100 characters, no control or invisible characters. */
11export function checkPerson(person: { id: unknown; name: unknown }): PersonSender {
12 const plain = (v: unknown, what: string): string => {
13 if (
14 typeof v !== "string" ||
15 v.trim() === "" ||
16 v.length > 100 ||
17 /[\x00-\x1f\x7f\u200b-\u200f\u202a-\u202e\u2066-\u2069\ufeff]/.test(v)
18 ) {
19 throw new ProjectError(
20 "bad-request",
21 `the person's ${what} must be 1-100 characters, with no control or invisible characters`,
22 );
23 }
24 return v.trim();
25 };
26 return { person: { id: plain(person?.id, "id"), name: plain(person?.name, "name") }, via: "website" };
29export function countRefusals(list: readonly Refusal[]): RefusalCounts {
30 const counts: RefusalCounts = { total: list.length, lines: 0, definition: 0, attribute: 0, duplicate: 0 };
31 for (const r of list) for (const kind of new Set(r.collisions.map((c) => c.kind))) counts[kind]++;
32 return counts;
35/** The project file's names, the one read first first (224): abso.json, then avcs.json from before the rename. */
36export const PROJECT_FILES = ["abso.json", "avcs.json"] as const;
38/**
39 * The project file a set of files holds: abso.json when it has one, else
40 * avcs.json. `ignored` names avcs.json when both are there, so status can say
41 * which one counts.
42 */
43export function projectFileIn(files: {
44 hasFile(path: string): boolean;
45 text(path: string): string;
46}): { name: (typeof PROJECT_FILES)[number]; text: string; ignored?: "avcs.json" } | undefined {
47 const [name, other] = PROJECT_FILES.filter((n) => files.hasFile(n));
48 if (!name) return undefined;
49 return { name, text: files.text(name), ...(other ? { ignored: "avcs.json" as const } : {}) };
52/** projectFileIn for a plain path -> text record (init, git intake). */
53export function projectFileOf(files: Readonly<Record<string, unknown>>): ReturnType<typeof projectFileIn> {
54 return projectFileIn({ hasFile: (p) => typeof files[p] === "string", text: (p) => files[p] as string });
57/**
58 * What's wrong with a project file (abso.json or avcs.json), or undefined:
59 * it must be a JSON object, and the keys abso reads must have their types:
60 * "whole" a list of globs, "queue" true or false, "review" a list of
61 * {paths, reviewer}, CI's "setup", "build", "check" and "out" strings, and
62 * "gitIssues" (where "#N" in history imported from git points) a web address.
63 */
64export function configProblem(text: string): string | undefined {
65 let config: unknown;
66 try {
67 config = JSON.parse(text);
68 } catch (e) {
69 return `isn't valid JSON (${e instanceof Error ? e.message : String(e)})`;
70 }
71 if (typeof config !== "object" || config === null || Array.isArray(config)) return "must be a JSON object";
72 const c = config as Record<string, unknown>;
73 if (c.whole !== undefined && (!Array.isArray(c.whole) || !c.whole.every((p) => typeof p === "string")))
74 return `has "whole" that isn't a list of path globs`;
75 if (c.queue !== undefined && typeof c.queue !== "boolean") return `has "queue" that isn't true or false`;
76 if (c.review !== undefined) {
77 const ok =
78 Array.isArray(c.review) &&
79 c.review.every((r) => {
80 const { paths, reviewer } = (r ?? {}) as { paths?: unknown; reviewer?: unknown };
81 return (
82 Array.isArray(paths) &&
83 paths.every((p) => typeof p === "string") &&
84 typeof reviewer === "string" &&
85 reviewer !== ""
86 );
87 });
88 if (!ok) return `has "review" that isn't a list of {"paths": [globs], "reviewer": "<agent>"}`;
89 }
90 if (c.gitIssues !== undefined && (typeof c.gitIssues !== "string" || !/^https?:\/\/\S+$/.test(c.gitIssues)))
91 return `has "gitIssues" that isn't a web address (like "https://github.com/owner/repo/issues")`;
92 for (const key of ["setup", "build", "check", "out"])
93 if (c[key] !== undefined && typeof c[key] !== "string") return `has "${key}" that isn't a string`;
94 return undefined;
97/** What a kept row of a version's files must match (#41): the version as the log holds it. */
98export function shapeStamp(p: Published): string {
99 return `${p.agent} ${p.time} ${p.atoms.length} ${p.atoms[p.atoms.length - 1] ?? ""} ${p.dropped?.length ?? 0}`;
102export function checkDraftName(name: unknown): string {
103 if (typeof name !== "string" || !NAME.test(name)) {
104 throw new ProjectError(
105 "bad-request",
106 `bad draft name ${JSON.stringify(name)}: letters, digits, '.', '_' and '-', up to 64`,
107 );
108 }
109 return name;
112/** Replace the one occurrence of `old` in `text`, or undefined when it isn't there exactly once. */
113export function replaceOnce(text: string, old: unknown, replacement: unknown): string | undefined {
114 if (typeof old !== "string" || typeof replacement !== "string") {
115 throw new ProjectError("bad-request", "each edit needs old and new strings");
116 }
117 const o = normalizeText(old);
118 if (o === "") return undefined;
119 const at = text.indexOf(o);
120 if (at < 0 || text.indexOf(o, at + 1) >= 0) return undefined;
121 return text.slice(0, at) + normalizeText(replacement) + text.slice(at + o.length);
124export function pairKey(p: Pair): string {
125 return `${p.yours} ${p.theirs}`;
128export function sortKeys<T>(obj: Record<string, T>): Record<string, T> {
129 const out: Record<string, T> = {};
130 for (const k of Object.keys(obj).toSorted()) out[k] = obj[k]!;
131 return out;
134/** A string's length in UTF-8 bytes, without encoding it (Sizes in a folder, #41). */
135export function utf8Bytes(s: string): number {
136 let n = 0;
137 for (let i = 0; i < s.length; i++) {
138 const c = s.charCodeAt(i);
139 if (c < 0x80) n += 1;
140 else if (c < 0x800) n += 2;
141 else if (c >= 0xd800 && c < 0xdc00 && i + 1 < s.length) {
142 n += 4;
143 i++;
144 } else n += 3;
145 }
146 return n;