UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

108 lines (107 loc) • 6.42 kB
import { isRemoteExecutionEnv } from "../remote-env.js"; import { validatePath } from "../effective-path-target.js"; import { installPolicyAnchors, installationAnchorsOf, installedFrom } from "../path-rule-binding.js"; import { CheckpointError } from "../checkpoint-store.js"; export function deploymentCanRerootChild(deps) { return deps.executionEnvFactory !== undefined; } export async function childPlacementOf(env, childRoot, asked, callerRoot) { const canon = async (p) => { try { const r = await env.canonicalPath(p); return r.ok ? r.value : p; } catch { return p; } }; const root = await canon(childRoot); if (asked !== undefined && (await canon(asked)) === root) return "honored"; const tree = await canon(callerRoot); const sep = tree.includes("\\") && !tree.includes("/") ? "\\" : "/"; return root === tree || root.startsWith(tree.endsWith(sep) ? tree : tree + sep) ? "caller_tree" : "elsewhere"; } export function childPlacementOnLeg(env, internals, root) { const asked = internals.isolation === "worktree" ? (isRemoteExecutionEnv(env) ? undefined : root) : internals.parentCwd; return childPlacementOf(env, root, asked, internals.callerRoot); } export function worktreeNotHonored(cwd) { return Object.assign(new Error(`isolation "worktree" was requested but cannot be honored: the execution environment factory placed the sub-agent at "${cwd}", inside the caller's own tree, instead of the worktree it was offered. The sub-agent was not started (fail-closed — no silent fallback to the shared working tree).`), { code: "isolation.unavailable" }); } const AS_RECORDED = (p) => p; function recordedAnchors(r, rebase) { if (r === null || r === undefined) return undefined; const read = (v) => { if (typeof v !== "string") return undefined; const p = rebase(v); return validatePath(p, { intent: "validate-coordinate" }); }; const [root, home] = [read(r.root), read(r.home)]; return { ...(root !== undefined ? { root } : {}), ...(home !== undefined ? { home } : {}) }; } export function chainTopAnchors(cfg) { if (cfg.placement === "elsewhere") return cfg.leg; return (cfg.live !== undefined ? recordedAnchors(cfg.live, AS_RECORDED) : recordedAnchors(cfg.seed, cfg.rebase)) ?? cfg.leg; } const GAP_REASON = { absent: "installation_record_absent", misaligned: "installation_record_misaligned", invalid: "installation_record_invalid" }; function dependsOnInstallation(policy) { if (installedFrom(policy) !== undefined) return false; return typeof policy.pathRules === "function" && policy.pathRules({}).some((row) => row.unanchored !== undefined); } function recordedInstallation(installations, chainLength, position, rebase) { const [record, read] = installations.live !== undefined ? [installations.live, AS_RECORDED] : [installations.seed, rebase]; if (record === undefined) return "absent"; if (record.length !== chainLength) return "misaligned"; const a = recordedAnchors(record[position], read); return a?.root === undefined ? "invalid" : a; } export function chainInstallationGaps(chain, installations, rebase) { return chain.flatMap((policy, position) => { if (!dependsOnInstallation(policy)) return []; const r = recordedInstallation(installations, chain.length, position, rebase); return typeof r === "string" ? [{ position, gap: r }] : []; }); } export function chainInstallationRefusal(gaps, pending) { const first = gaps[0]; const form = { absent: "absent (a chain written before installation records existed, or one built without them)", misaligned: "misaligned (it does not cover the chain entry for entry)", invalid: "invalid for that entry (it names no absolute root)" }[first.gap]; return new CheckpointError("resume.parent_constraint_mismatch", `the parent-constraint chain carries ${gaps.length} polic${gaps.length === 1 ? "y" : "ies"} (entry ${gaps.map((g) => g.position).join(", ")}) whose path rules resolve against the leg that installed ${gaps.length === 1 ? "it" : "them"}, and the installation record is ${form} — installing ${gaps.length === 1 ? "it" : "them"} anywhere else would move what ${gaps.length === 1 ? "it protects" : "they protect"}, so the chain is refused` + (pending ? " before the claim (the checkpoint stays pending). A task parked before 7.46.0 under such a chain cannot be resumed by this version: let a worker of the version that parked it finish it, or let it expire and submit the task again." : "."), { reason: GAP_REASON[first.gap] }); } export function chainPolicyOnLeg(cfg) { if (cfg.placement !== "elsewhere") { const gaps = chainInstallationGaps(cfg.chain, cfg.installations, cfg.rebase); if (gaps.length > 0) throw chainInstallationRefusal(gaps, false); } const recorded = (position) => { const r = recordedInstallation(cfg.installations, cfg.chain.length, position, cfg.rebase); return typeof r === "string" ? undefined : r; }; const chained = (policy, position) => (cfg.placement === "elsewhere" ? installPolicyAnchors(policy, cfg.leg) : installedFrom(policy) !== undefined ? policy : installPolicyAnchors(policy, recorded(position) ?? cfg.chainTop)) ?? policy; const own = (policy) => { if (policy === undefined) return undefined; const position = cfg.placement === "elsewhere" ? -1 : cfg.chain.findIndex((c) => (installedFrom(c) ?? c) === (installedFrom(policy) ?? policy)); return position >= 0 ? chained(cfg.chain[position], position) : installPolicyAnchors(policy, cfg.leg); }; return { own, chained }; } export function installationRecordOf(policy) { const a = installationAnchorsOf(policy); return a?.root === undefined ? null : { root: a.root, ...(a.home !== undefined ? { home: a.home } : {}) }; } export function chainAnchorRecords(chainTop, chain) { return { ...(chainTop.root !== undefined ? { pathRuleAnchors: { root: chainTop.root, ...(chainTop.home !== undefined ? { home: chainTop.home } : {}) } } : {}), ...(chain.some((pc) => installationAnchorsOf(pc.policy)?.root !== undefined) ? { pathRuleInstallations: chain.map((pc) => installationRecordOf(pc.policy)) } : {}), }; }