@sema-agent/core
Version:
Stateless, task-oriented AI agent core
35 lines (34 loc) • 1.49 kB
JavaScript
import { isPlainRecord, safeDataValue } from "./checkpoint-store.js";
import { hasWin32AliasedComponent, isAbsolutePathForm, normalizeAbsPathLexically, pathFamilyOf, win32NamespaceScreen } from "../tools/fs/safety.js";
export const READ_ROOT_CANDIDATE_DIR_MAX = 1024;
export function normalizeReadRootCandidate(value) {
if (!isPlainRecord(value))
return undefined;
if (safeDataValue(value, "clearsThisAsk") !== true)
return undefined;
const dir = safeDataValue(value, "dir");
if (typeof dir !== "string" || dir.length === 0 || dir.length > READ_ROOT_CANDIDATE_DIR_MAX)
return undefined;
if (/[\u0000-\u001f\u007f\u2028\u2029]/.test(dir))
return undefined;
if (!isAbsolutePathForm(dir))
return undefined;
const folded = normalizeAbsPathLexically(dir);
if (folded !== dir || normalizeAbsPathLexically(`${dir}/..`) === dir)
return undefined;
if (hasWin32AliasedComponent(dir))
return undefined;
if (!win32NamespaceScreen(dir, pathFamilyOf({ root: dir })).ok)
return undefined;
let covers;
try {
const d = Object.getOwnPropertyDescriptor(value, "covers");
covers = d === undefined ? "absent" : "value" in d && d.value === "exact" ? "exact" : "refuse";
}
catch {
covers = "refuse";
}
if (covers === "refuse")
return undefined;
return covers === "exact" ? { dir, clearsThisAsk: true, covers: "exact" } : { dir, clearsThisAsk: true };
}