UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

22 lines (21 loc) • 1.25 kB
import { shellQuote } from "../../tools/fs/search.js"; const CWD_ALIGNMENT_MARK = "cwd-alignment:"; export function cwdAlignmentProbe(taskRoot) { const root = shellQuote(taskRoot); return `(p=$(pwd -P 2>/dev/null || pwd); if test -d ${root} >/dev/null 2>&1; then test . -ef ${root} >/dev/null 2>&1; case $? in 0) v=same;; 1) v=differ;; *) v=unknown;; esac; else v=unknown; fi; printf '%s\\n' "${CWD_ALIGNMENT_MARK}$v:$p")`; } export function readCwdAlignment(lines) { for (let i = lines.length - 1; i >= 0; i--) { const line = lines[i] ?? ""; if (!line.startsWith(CWD_ALIGNMENT_MARK)) continue; const rest = line.slice(CWD_ALIGNMENT_MARK.length); const cut = rest.indexOf(":"); const word = cut < 0 ? rest : rest.slice(0, cut); return { verdict: word === "same" || word === "differ" ? word : "unknown", shellPwd: cut < 0 ? "" : rest.slice(cut + 1).trim() }; } return undefined; } export function cwdMisalignmentMessage(taskRoot, shellPwd) { return `execution-env cwd misalignment: a probe run with the task root "${taskRoot}" as its working directory reports pwd "${shellPwd}", and the env's shell found that directory to be a different one from the task root`; }