UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

36 lines (35 loc) • 1.98 kB
export function twinsAfter(twins, si, at, movedFrom, max) { if (twins === undefined || at.shift.kind === "lost") return twins; if (at.shift.kind === "eval") return undefined; if (at.shift.kind === "stack") return twins.map(({ dirStack: _, ...c }) => c); const table = at.table; const twinAt = { ...at, table: { ...table, stateOf: (name) => { const s = table.stateOf(name); const f = s === "unknown" ? table.floorOf(name).values.filter((v) => v.glob !== true && v.piece !== true) : []; return f.length === 0 ? s : f.map((v) => ({ spelled: v.spelled })); } } }; const byKey = new Map(); for (const c of twins) { const next = [{ ...c, skippedCd: at.provable ? si : c.skippedCd }]; if (c.skippedCd < at.andRunStart) { const moved = movedFrom(c, twinAt); if (moved !== "lost") next.push(...moved.map((m) => ({ ...m, skippedCd: c.skippedCd }))); } for (const n of next) { const key = JSON.stringify([n.base, n.pwd, n.old, n.oldFloor, n.dirStack]); const prior = byKey.get(key); if (prior === undefined || n.skippedCd < prior.skippedCd) byKey.set(key, n); } } return byKey.size > max ? undefined : [...byKey.values()]; } export function twinFloor(twins, andRunStart) { const reached = twins?.filter((c) => c.skippedCd < andRunStart); if (reached === undefined) return undefined; const spellings = (pick) => [...new Set(reached.flatMap((c) => pick(c) ?? []))]; const depth = Math.max(0, ...reached.map((c) => c.dirStack?.length ?? 0)); const floor = { PWD: spellings((c) => c.pwd), OLDPWD: spellings((c) => c.old ?? c.oldFloor), DIRSTACK: Array.from({ length: depth + 1 }, (_, k) => spellings((c) => (k === 0 ? c.pwd : c.dirStack?.[k - 1]))) }; return floor.PWD.length > 0 || floor.OLDPWD.length > 0 || floor.DIRSTACK.some((e) => e.length > 0) ? floor : undefined; }