UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

97 lines (96 loc) • 3.54 kB
import { isSessionConflict } from "../session.js"; import { thrownText } from "../thrown-value.js"; import { isHolderSuperseded } from "../grant-holder.js"; import { settleTeardownLeg } from "./teardown-bounded.js"; export function writeRefusalLatch() { return { word: undefined }; } export function latchWriteRefusal(ref, err) { if (isHolderSuperseded(err)) { if (ref.word === "holder.superseded") return; ref.word = "holder.superseded"; try { ref.stop?.(); } catch { } } else if (isSessionConflict(err) && ref.word === undefined) { ref.word = "conflict"; } } export function detectConflicts(storage, ref, own) { const guard = (fn, landed) => async (...args) => { try { const out = await fn(...args); landed(args); return out; } catch (err) { latchWriteRefusal(ref, err); throw err; } }; return new Proxy(storage, { get(target, prop, receiver) { const value = Reflect.get(target, prop, receiver); if (typeof value !== "function") { return value; } const bound = value.bind(target); if (prop === "appendEntry") return guard(bound, (args) => own.ids?.add(args[0].id)); if (prop === "setLeafId") return guard(bound, () => undefined); return bound; }, }); } export async function writeBranchBack(branch, sessionId, signal) { const state = (word) => { if (branch.outcome !== undefined) branch.outcome.branch = word; }; let now; let moved = false; try { now = await branch.storage.getLeafId(); if (now === branch.leafBefore) return state(branch.leafSet === branch.leafBefore ? "untouched" : "put_back"); const ids = branch.own.ids ?? new Set(); for (let at = now, steps = 0; at !== branch.leafSet; steps++) { if (at === null || steps > ids.size || !ids.has(at)) { moved = true; throw new Error("another writer moved the leaf past the entries this request wrote"); } at = (await branch.storage.getEntry(at))?.parentId ?? null; } if (signal?.aborted === true) return state("left_moved"); state(undefined); await branch.storage.setLeafId(branch.leafBefore, { expectedLeafId: now }); state("put_back"); } catch (err) { if (moved) state("left_moved"); else if (isSessionConflict(err)) { try { state((await branch.storage.getLeafId()) === branch.leafBefore ? "put_back" : "left_moved"); } catch { state(undefined); } } else state(undefined); const shown = (id) => (id === undefined ? "(unreadable)" : id === null ? "(empty)" : `"${id}"`); throw new Error(`resumeAt: prepare refused this request but could not put session "${sessionId}" back — its leaf stays at ${shown(now)} (the request found it at ${shown(branch.leafBefore)}), so the next turn continues from there: ${thrownText(err)}`); } } export async function settleBranchWriteBack(branch, sessionId, label, report) { const ac = new AbortController(); await settleTeardownLeg(() => writeBranchBack(branch, sessionId, ac.signal), label, report); ac.abort(); }