UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

80 lines (79 loc) • 3.77 kB
import { driveRelativeReadingsOf, isAbsoluteForFamily, isIdentityDir, isUncIdentity, pathFamilyOf, pathIdentityOf, shellRootedLandingsOf, shellWalkLeavesDrive } from "./safety.js"; import { resolveOperandSpellings } from "./bash-readonly-classifier.js"; const CLIMB_LANDINGS_MAX = 16; const PARENT_STEP = /(?:^|[\\/])\.\.(?:[\\/]|$)/; export function readingsAt(base, spelled) { return (base === "" ? undefined : driveRelativeReadingsOf(pathFamilyOf({ root: base }), base, spelled)) ?? [[base, spelled]]; } export function noteClimb(family, at, rest, climbs) { const id = family === "win" ? pathIdentityOf(at, "win") : undefined; if (id === undefined || !id.ok || isUncIdentity(id.identity) || isIdentityDir(id.identity, "win")) return false; const emulation = `/${rest.join("/")}`; if (!climbs.includes(emulation)) climbs.push(emulation); return true; } export async function landingsOf(climbs, each, bounded) { const seen = new Set(); for (let k = 0; k < climbs.length; k++) { if (k >= CLIMB_LANDINGS_MAX) return void bounded(`a path climbs above a drive's root more than ${CLIMB_LANDINGS_MAX} times, too many readings to follow to verify where it leads`); for (const landing of shellRootedLandingsOf(climbs[k])) { if (seen.has(landing)) continue; seen.add(landing); await each(landing); } } } export function withClimbedViews(kept, other) { const spellings = other.spellings.filter((s) => !kept.spellings.includes(s)); const hops = (other.hops ?? []).filter((h) => !(kept.hops ?? []).includes(h)); return spellings.length === 0 && hops.length === 0 ? kept : { ...kept, spellings: [...kept.spellings, ...spellings], ...(hops.length > 0 ? { hops: [...(kept.hops ?? []), ...hops] } : {}) }; } async function climbsOf(family, spellings, walk, signal) { const climbs = []; if (family !== "win") return climbs; try { for (const s of spellings) if (isAbsoluteForFamily("win", s) && PARENT_STEP.test(s)) await walk(s, climbs); } catch (e) { if (signal?.aborted === true) throw e; } return climbs; } export async function withClimbedPatterns(family, patterns, walk, bounded, signal) { const out = [...patterns]; await landingsOf(await climbsOf(family, patterns, walk, signal), async (landing) => void out.push(landing), bounded); return out; } export async function withFloorClimbs(family, read, walk, bounded, signal) { if (family !== "win" || read.established !== "unestablishable" || read.candidate.bases !== "unmodelled") return read; const climbs = await climbsOf(family, read.spellings, walk, signal); const added = []; try { await landingsOf(climbs, async (landing) => { const real = await walk(landing, climbs); added.push(landing, ...(real !== undefined && (real.exact || real.missing === true) ? [real.value] : [])); }, bounded); } catch (e) { if (signal?.aborted === true) throw e; } const views = [...new Set(added)].filter((c) => !read.spellings.includes(c)); return views.length === 0 ? read : { ...read, spellings: [...read.spellings, ...views] }; } export function movedTo(base, target, homeDir, typed) { const at = resolveOperandSpellings(base, target, homeDir); if (at === undefined || at.standIn === true) return undefined; const family = at.spelled === undefined ? undefined : pathFamilyOf({ root: at.spelled }); return at.spelled !== undefined && !(typed && isAbsoluteForFamily(family ?? "own", target)) && shellWalkLeavesDrive(family, at.spelled) ? at.spelled : at.folded; }