UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

56 lines • 2.71 kB
import { defer, refuse } from "./verdict.js"; const SHELL_NAMES = new Set(["bash", "sh", "zsh", "dash", "ksh", "mksh", "ash", "yash", "posh", "fish", "csh", "tcsh"]); /** Wrappers that keep `kill` a shell builtin; every other wrapper execs the external `kill`. */ const BUILTIN_KEEPING = new Set(["command", "builtin", "time", "eval", "trap"]); const wordsText = (words) => words.map((word) => word.text); /** A wrapper after which the command runs with a PATH the runtime layer's shims are not on (DESIGN-12096 R5). */ function resetsPath(wrapper) { const texts = wordsText(wrapper.words); switch (wrapper.name) { case "sudo": case "doas": case "chroot": case "systemd-run": return true; case "env": return texts.some((text) => text === "-i" || text === "--ignore-environment" || text === "-" || text.startsWith("PATH=")); case "su": return texts.some((text) => text === "-" || text === "-l" || text === "--login"); case "nix-shell": return texts.includes("--pure"); default: return false; } } export function situationOf(segment, context) { const resetPath = segment.wrappers.some(resetsPath); let shell = context.shell; let afterShell = segment.wrappers; for (let index = segment.wrappers.length - 1; index >= 0; index -= 1) { const wrapper = segment.wrappers[index]; if (wrapper !== undefined && SHELL_NAMES.has(wrapper.name)) { shell = wrapper.name; afterShell = segment.wrappers.slice(index + 1); break; } } const shellRuns = shell !== "bun" && shell !== "argv"; const builtin = segment.name === "kill" && !segment.nameIsPath && shellRuns && afterShell.every((wrapper) => BUILTIN_KEEPING.has(wrapper.name)); const runtimeCovers = !resetPath && !segment.nameIsPath && (!builtin || shell === "bash"); return { segment, resetPath, builtin, runtimeCovers }; } /** The verdict for a dynamic word: defer where the runtime layer will see it, refuse UNKNOWN where nothing will. */ export function dynamicVerdict(situation, what) { if (situation.runtimeCovers) return defer(`${what} is only known when the command runs`); const why = situation.resetPath ? "a wrapper resets PATH" : situation.segment.nameIsPath ? "the command is named by path" : "a shell builtin outside bash"; return refuse("R5", `\`${situation.segment.nameSpelling}\` gets ${what} that is only known when it runs, and nothing judges it then (${why})`, "Write the value out literally."); } //# sourceMappingURL=situation.js.map