UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

124 lines (123 loc) • 6.57 kB
import { carriesCredential, readSyncRefusal } from "./sync-client.js"; import { egressText, egressThrownText } from "../untrusted-egress.js"; import { isReplicableScope } from "./scope-contract.js"; const OUTCOMES = new Set(["synced", "incomplete", "refused", "failed"]); const text = (s) => egressText("same-principal", s); function requireNamed(what, v) { if (typeof v !== "string" || v.trim().length === 0) throw new TypeError(`memory sync report: ${what} must be a non-empty string`); if (carriesCredential(v)) throw new TypeError(`memory sync report: ${what} carries a credential — a name the report carries verbatim never does`); return v; } const appliedOf = (ops) => ops.map((op) => (op.slug !== undefined ? { ...op, slug: text(op.slug) } : { ...op })); function requireNames(what, v) { if (!Array.isArray(v)) throw new TypeError(`memory sync report: ${what} must be an array of scope keys`); const out = []; for (const s of v) { const key = requireNamed(`each of ${what}`, s); if (!out.includes(key)) out.push(key); } return out; } function hasExactlyOne(v, a, b, what) { const hasA = Object.prototype.hasOwnProperty.call(v, a); if (hasA === Object.prototype.hasOwnProperty.call(v, b)) throw new TypeError(`memory sync report: ${what} carries exactly one of { ${a} } and { ${b} }`); return hasA; } function callReport(call) { const scope = call.scope; if (!hasExactlyOne(call, "result", "thrown", `layer ${JSON.stringify(scope)}`)) { const thrown = call.thrown; const refusal = readSyncRefusal(thrown); const applied = { server: [], local: [] }; if (refusal !== undefined) { return { scope, outcome: "refused", applied, code: refusal.code, ...(refusal.detail !== undefined ? { detail: refusal.detail } : {}), outs: refusal.outs, message: refusal.message }; } return { scope, outcome: "failed", applied, error: egressThrownText("same-principal", thrown) }; } const r = call.result; const applied = { server: appliedOf(r.serverApplied), local: appliedOf(r.localApplied) }; const warnings = (r.warnings ?? []).map(text); if (r.ok && r.pushTruncated !== true && r.pullTruncated !== true && warnings.length === 0) return { scope, outcome: "synced", applied }; return { scope, outcome: "incomplete", why: r.ok ? "truncated" : "conflicts", applied, conflicts: r.conflicts.map((c) => ({ ...c, reason: text(c.reason) })), ...(warnings.length > 0 ? { warnings } : {}), }; } function foldCall(before, next) { return { ...next, applied: { server: [...before.applied.server, ...next.applied.server], local: [...before.applied.local, ...next.applied.local] } }; } export function memorySyncReportOf(round) { const target = requireNamed("target", round.target); const sourceRevision = requireNamed("sourceRevision", round.sourceRevision); const scopes = requireNames("scopes", round.scopes); if (!hasExactlyOne(round, "layers", "refused", "a sync")) { const refused = round.refused; if (refused?.code === "config.memory_sync_target_mismatch") { const reaches = requireNamed("refused.reaches", refused.reaches); const message = text(`memory sync: the session goes to ${JSON.stringify(target)}, and this machine's memory sync reaches ${JSON.stringify(reaches)} — ` + "no layer was synced: memory sent there would not be where the session reads it"); return { target, sourceRevision, scopes, refused: { code: refused.code, message } }; } if (refused?.code === "session_active") { return { target, sourceRevision, scopes, refused: { code: refused.code, message: text(requireNamed("refused.message", refused.message)) } }; } throw new TypeError(`memory sync report: refused.code must be "config.memory_sync_target_mismatch" or "session_active"`); } const calls = round.layers; if (!Array.isArray(calls)) throw new TypeError("memory sync report: layers must be an array"); const byScope = new Map(); for (const call of calls) { const scope = requireNamed("each layer's scope", call?.scope); if (!scopes.includes(scope)) throw new TypeError(`memory sync report: layer ${JSON.stringify(scope)} is not in the session's read set`); const report = callReport(call); const before = byScope.get(scope); byScope.set(scope, before === undefined ? report : foldCall(before, report)); } return { target, sourceRevision, scopes, layers: [...byScope.values()] }; } export function memoryTravelGapOf(report, readAtDestination) { if (typeof report !== "object" || report === null) throw new TypeError("memory travel gap: report must be a memory sync report"); const scopes = requireNames("report.scopes", report.scopes); const destination = readAtDestination === undefined ? undefined : new Set(requireNames("readAtDestination", readAtDestination)); const localOnly = (scope) => !isReplicableScope(scope); if (!hasExactlyOne(report, "layers", "refused", "a sync report")) return scopes.map((scope) => ({ scope, why: localOnly(scope) ? "local_only" : "refused" })); const layers = report.layers; if (!Array.isArray(layers)) throw new TypeError("memory travel gap: report.layers must be an array"); const byScope = new Map(); for (const layer of layers) { const scope = requireNamed("each layer's scope", layer?.scope); const outcome = layer.outcome; if (!scopes.includes(scope) || byScope.has(scope) || typeof outcome !== "string" || !OUTCOMES.has(outcome)) { throw new TypeError(`memory travel gap: layer ${JSON.stringify(scope)} is not one this engine reports (outside the read set, listed twice, or outcome ${JSON.stringify(outcome)})`); } byScope.set(scope, layer); } const gap = []; for (const scope of scopes) { const layer = byScope.get(scope); if (localOnly(scope)) gap.push({ scope, why: "local_only" }); else if (layer === undefined) gap.push({ scope, why: "not_replicated" }); else if (layer.outcome !== "synced") gap.push({ scope, why: layer.outcome }); else if (destination !== undefined && !destination.has(scope)) gap.push({ scope, why: "not_read_at_destination" }); } return gap; }