@sema-agent/core
Version:
Stateless, task-oriented AI agent core
70 lines (69 loc) • 3.06 kB
JavaScript
import { isDeviceSpelling, localDriveSpellingOf } from "./unc-spelling.js";
export function viewSpellings(views) {
const out = [];
for (const v of views.views)
if (!out.includes(v.path))
out.push(v.path);
return out;
}
export function spelledViews(spellings, kind) {
return { views: spellings.map((path, i) => ({ path, kind: kind ?? (i === 0 ? "lexical" : "canonical") })) };
}
const GUARD_HIT_VIA = { lexical: "spelling", canonical: "target", hop: "chain", home: "spelling" };
export function guardHitVia(kind) {
return kind === undefined ? "spelling" : GUARD_HIT_VIA[kind];
}
const CHECKOUT_READING = { lexical: "every-spelling", canonical: "canonical-only", hop: "every-spelling", home: "every-spelling" };
export function checkoutReadingOf(kind) {
return CHECKOUT_READING[kind];
}
export function guardHitReachClause(via) {
return via === "spelling" ? undefined : "matched where the path resolves on disk (its real target or a link on the way)";
}
export function judgePathRows(rows, views, direction) {
if (direction === "allow") {
if (views.unreadable !== undefined || views.views.length === 0)
return undefined;
for (const row of rows)
if (views.views.every((v) => row.reach(v).reach === "reached"))
return { row };
return undefined;
}
return judgePathRowsAsRead(rows, views) ?? tightenOver(rows, views, (view, judged) => (judged === view ? driveReadingOf(view) : []));
}
export function judgePathRowsAsRead(rows, views) {
return tightenOver(rows, views, (_view, judged) => [judged]);
}
function tightenOver(rows, views, readingsOf) {
let held;
for (const row of rows) {
if (views.unreadable !== undefined)
held ??= { row, unreadable: views.unreadable };
for (const view of views.views) {
const judged = row.exemptible === true ? exemptReading(view, views) : view;
if (judged === undefined)
continue;
for (const at of readingsOf(view, judged)) {
const r = row.reach(at);
if (r.reach === "reached")
return { row, view: at.path, viewKind: view.kind };
if (r.reach === "unreadable")
held ??= { row, unreadable: r.reason };
}
}
}
return held;
}
function driveReadingOf(view) {
const drive = isDeviceSpelling(view.path) ? undefined : localDriveSpellingOf(view.path);
return drive === undefined || drive === view.path ? [] : [{ path: drive, kind: view.kind, ...(view.aliasResolved === true ? { aliasResolved: true } : {}) }];
}
function exemptReading(view, views) {
if (views.identityExempt === true && view.kind !== "canonical")
return undefined;
if (view.exemptRemainder === undefined)
return view;
if (view.exemptRemainder === "")
return undefined;
return { path: view.exemptRemainder, kind: view.kind, ...(view.aliasResolved === true ? { aliasResolved: true } : {}) };
}