@sema-agent/core
Version:
Stateless, task-oriented AI agent core
133 lines (132 loc) • 6.45 kB
JavaScript
import { literalTildeSpelling } from "../../shell/lex.js";
import { resolveOperandLexically, resolveOperandSpellings } from "./bash-readonly-classifier.js";
import { readArgRoles } from "./program-read-model.js";
import { anchorSpelling, normalizeAbsPathLexically } from "./safety.js";
import { isAbsence } from "./shell-read-environment.js";
const STARTS_IN = "-C";
const NAMES_TOP = "--work-tree";
const NAMES_REPOSITORY = new Set(["--git-dir", "--bare"]);
export function objectPathRepositoryOf(row, globals) {
const words = globals.map((r) => r.folded);
const settled = (r, value) => (r.expands === false && (r.values?.length ?? 1) <= 1 ? value : undefined);
const moves = [];
let workTree;
let named = false;
for (const e of row === undefined ? [] : readArgRoles(row, words)) {
if (e.kind !== "value" && e.kind !== "option")
continue;
const option = e.kind === "value" ? words[e.of] : words[e.at].split("=")[0];
const value = e.kind === "value" ? settled(globals[e.at], words[e.at]) : typeof e.reading === "object" ? settled(globals[e.at], literalTildeSpelling(e.reading.attached)) : undefined;
if (option === STARTS_IN)
moves.push(value);
else if (option === NAMES_TOP)
workTree = value ?? null;
else if (NAMES_REPOSITORY.has(option))
named = true;
}
return { moves, ...(workTree !== undefined ? { workTree } : {}), ...(named ? { named: true } : {}) };
}
export function mayCarryGitVariables(...names) {
return names.some((part) => part === "all" || [...part].some((n) => n.startsWith("GIT_")));
}
export function chdirFrom(bases, spelling, homeDir) {
const out = bases.map((b) => resolveOperandSpellings(b, spelling, homeDir)).map((s) => (s?.standIn === true ? undefined : (s?.spelled ?? s?.folded)));
return out.every((p) => p !== undefined) ? [...new Set(out)] : undefined;
}
export function chdirMayLeadTo(bases, spelling, homeDir) {
return [...new Set(bases.flatMap((b) => {
const s = resolveOperandSpellings(b, spelling, homeDir);
return s?.standIn === true ? [s.spelled ?? s.folded] : [];
}))];
}
export function objectPathPlacement(spelled, repository, at, floor, homeDir, variables) {
const dotted = /^\.\.?(?:\/|$)/.test(spelled);
const mayLead = new Set();
const place = (bases, word) => {
const to = chdirFrom(bases, word, homeDir);
if (to === undefined)
for (const p of chdirMayLeadTo(bases, word, homeDir))
mayLead.add(p);
return to;
};
const unplaced = (top) => {
const floors = [...new Set([...((at === "unmodelled" ? floor : at) ?? []), ...mayLead])];
return { bases: "unmodelled", ...(floors.length > 0 ? { floorBases: floors } : {}), ...(top !== undefined ? { repositoryTop: top } : {}) };
};
let starts = at === "unmodelled" ? undefined : at;
for (const move of repository?.moves ?? [])
if (starts !== undefined && move !== "")
starts = move === undefined ? undefined : place(starts, move);
if (starts === undefined)
return unplaced(dotted ? undefined : "unknown");
if (dotted)
return { bases: starts };
const workTree = repository?.workTree;
if (workTree !== undefined) {
const tops = workTree === null ? undefined : place(starts, workTree);
return tops === undefined ? unplaced("unknown") : { bases: tops };
}
return { bases: starts, repositoryTop: repository?.named === true || variables ? "unknown" : "discovered" };
}
export function everyAncestor(dirs) {
const out = new Set(dirs);
for (const dir of dirs) {
for (let d = resolveOperandLexically(dir, ".", undefined) ?? dir;;) {
out.add(d);
const up = resolveOperandLexically(d, "..", undefined) ?? d;
if (up === d)
break;
d = up;
}
}
return [...out];
}
const READ_LIMIT = 65_536;
export async function repositoryTopOf(fs, dir, family, signal) {
const start = await fs.canonicalPath(dir, signal);
if (!start.ok)
return undefined;
const entry = async (path) => {
const info = await fs.fileInfo(path, signal);
if (!info.ok)
return isAbsence(info.error.code) ? "absent" : "unanswered";
return info.value.kind === "symlink" ? "link" : info.value.kind;
};
const text = async (path) => {
const info = await fs.fileInfo(path, signal);
if (!info.ok || info.value.kind !== "file" || info.value.size > READ_LIMIT)
return undefined;
const read = await fs.readTextFile(path, signal);
return read.ok ? read.value : undefined;
};
const isRepository = async (git) => {
for (const name of ["objects", "refs"]) {
const info = await fs.fileInfo(anchorSpelling(family, git, name), signal);
if (!info.ok || info.value.kind !== "directory" || (info.value.mode !== undefined && (info.value.mode & 0o100) === 0))
return false;
}
const head = anchorSpelling(family, git, "HEAD");
const headRef = (await entry(head)) === "link" ? await fs.readLink?.(head, signal).then((r) => (r.ok && r.value.startsWith("refs/") ? "ref: refs/" : undefined)) : await text(head);
if (headRef === undefined || !/^(?:ref:[ \t\n\r]*refs\/|[0-9a-fA-F]{40})/.test(headRef.slice(0, 255)))
return false;
const config = anchorSpelling(family, git, "config");
return (await entry(config)) === "absent" || ((await text(config)) ?? "worktree").search(/worktree/i) < 0;
};
for (let d = start.value;;) {
const git = anchorSpelling(family, d, ".git");
const found = await entry(git);
if (found === "directory")
return (await isRepository(git)) ? d : undefined;
if (found !== "absent")
return undefined;
const head = await entry(anchorSpelling(family, d, "HEAD"));
if (head === "unanswered")
return undefined;
if (head !== "absent" && ((await entry(anchorSpelling(family, d, "refs"))) !== "absent" || (await entry(anchorSpelling(family, d, "commondir"))) !== "absent"))
return undefined;
const up = normalizeAbsPathLexically(anchorSpelling(family, d, ".."));
if (up === d)
return undefined;
d = up;
}
}