UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

46 lines (45 loc) • 2.62 kB
import { mintCheckpointId } from "../checkpoint-store.js"; import { exactBoundInputHashOf } from "../canonical-json.js"; import { argsAsTheToolReads } from "../effective-path-target.js"; import { readRejudge } from "../rewrite-rejudge.js"; import { attestationOf, hostWindowUnanswered } from "../tool-policy.js"; export function deferredAskOf(resolved, call, checkpointId) { if (resolved.approverUnavailable !== true) return undefined; const inputDigest = attestationOf(resolved, call)?.inputDigest; return inputDigest !== undefined ? { checkpointId, inputDigest } : undefined; } export function parkedAskIdentity(deferred, rowDigest) { return deferred !== undefined && deferred.inputDigest === rowDigest ? deferred.checkpointId : mintCheckpointId(); } export const windowMarkOf = (resolved, call) => (hostWindowUnanswered(attestationOf(resolved, call)) ? { windowExpired: true } : {}); export function stationMarkOf(question, call, boundArgs = question.updatedInput !== undefined ? argsAsTheToolReads(call, question.updatedInput) : call.args) { return { question, inputDigest: exactBoundInputHashOf(call.toolName, boundArgs) }; } export function markCall(channel, station, toolCallId, mark) { channel.set(toolCallId, (channel.get(toolCallId) ?? new Map()).set(station, mark)); } export function liftMark(channel, station, toolCallId) { const marks = channel.get(toolCallId); if (marks?.delete(station) === true && marks.size === 0) channel.delete(toolCallId); } export function markBinds(mark, call, args) { return mark.inputDigest !== undefined && mark.inputDigest === exactBoundInputHashOf(call.toolName, args); } export async function stationLook(channel, station, call, look) { const prior = channel.get(call.toolCallId)?.get(station); const verdict = await look(); const standing = channel.get(call.toolCallId)?.get(station) === prior ? standingOf(channel, station, call) : undefined; return standing === undefined || verdict.action === "deny" || readRejudge(call, call.args, verdict).kind === "rewritten" ? verdict : standing; } export function standingOf(channel, station, call) { const mark = channel.get(call.toolCallId)?.get(station); if (mark === undefined) return undefined; const { updatedInput: _bound, ...question } = mark.question; return question; } export function movedClause(toolName) { return `the ancestor's question about this call to "${toolName}" was deferred over the arguments as they read before a later policy layer rewrote them; the arguments that would run were never shown to that approver`; }