UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

107 lines (106 loc) • 8.37 kB
import { ROOT_ANCHOR_DAMAGED, STORE_ABSENT, isRootAnchorRefusal, storeAbsence, storeFormRefusalReadingOf } from "./layout.js"; const OPERATOR_TAIL = "Whoever runs this deployment can do this (the service log names the exact step); the next run mounts it again."; const OUTSIDE = { act_outside: { clause: "a folder inside the memory store is a link that points to a place outside it", restore: "replace that link inside the memory store with a real directory", tail: OPERATOR_TAIL, }, root_moved: { clause: "the memory store's directory now leads to a different place than the one it was set up in", restore: "put the memory directory back where it was — or, if it was moved on purpose, confirm its new location", tail: OPERATOR_TAIL, }, root_gone: { clause: "the memory store's directory is no longer where it was set up", restore: "put the memory directory back — or, if it was moved on purpose, confirm its new location (to start over, create it again and confirm it there)", tail: OPERATOR_TAIL, }, root_reanchored: { clause: "the memory this conversation opened has been moved to another place since", restore: "start a new conversation, which opens it in its new place", tail: "Nothing else needs doing.", }, }; const CLAUSE = { "memory.scope_home_not_directory": "a memory scope's directory is a symbolic link or not a real directory", "memory.control_plane_links_unsupported": "the file system under the memory store cannot make hard links", "memory.control_plane_strays": "memory records from an earlier setup are still kept beside the current ones, and nothing reads them", "memory.store_root_sensitive": "the memory store is configured inside a directory the engine's write guard protects (a credential or access-control directory)", "memory.control_plane_split": "since the memory store's location was moved, what is kept beside it stands in two places that disagree", "memory.store_absent": "the memory store this deployment had is no longer where it was set up (its folder was moved away, or the disk it lives on is not mounted)", "memory.root_anchor_damaged": "the memory store was left by an older version of the engine, which did not record where the store lives — or the file that records it was removed or is damaged; the engine changed none of the store's memory and removed none of it", unclassified: "the memory store could not be opened", }; const RESTORE = { "memory.scope_home_not_directory": "replace that link or file with a real directory", "memory.control_plane_links_unsupported": "move the memory store to a disk that supports hard links", "memory.control_plane_strays": "merge the memory records the earlier setup left beside the current ones", "memory.store_root_sensitive": "point the memory store at a directory outside the protected one", "memory.control_plane_split": "keep one of the two copies of each record the service log names and remove the other", "memory.store_absent": "put the memory folder back where it was, or mount its disk (it opens again as it was); to start over with empty memory on purpose instead, start the service again — unless the service is set up to require this memory, which then has to be set up again as a new, empty memory", "memory.root_anchor_damaged": "re-anchor the store once — one call, given the store's directory and the directory kept beside it, both named in the service log (if that call is refused, its own message says what comes first); the store then opens with its memory", unclassified: "check the service log for why the memory store could not be opened", }; export function memoryMountFailedReadingOf(thrown) { const refusal = storeFormRefusalReadingOf(thrown); if (refusal === undefined) return { cause: isRootAnchorRefusal(thrown) ? ROOT_ANCHOR_DAMAGED : "unclassified" }; return "outsideReason" in refusal ? { cause: refusal.code, outsideReason: refusal.outsideReason } : { cause: refusal.code }; } function mountFailedWordsOf(reading) { return "outsideReason" in reading ? OUTSIDE[reading.outsideReason] : { clause: CLAUSE[reading.cause], restore: RESTORE[reading.cause], tail: OPERATOR_TAIL }; } export function memoryMountFailedNotice(input) { const words = mountFailedWordsOf(input); return { code: "memory.mount_failed", message: `Memory did not mount for this session: ${words.clause}. The task runs without it — stored memory is not available to the assistant, ` + `and nothing is saved into that memory store in this session. To restore it: ${words.restore}. ${words.tail}`, detail: { sessionId: input.sessionId, scopes: [...input.scopes], cause: input.cause, ...("outsideReason" in input ? { outsideReason: input.outsideReason } : {}) }, }; } export const CAPTURE_OPTOUT_UNPERSISTED_RETRY = Object.freeze({ record_unpersisted: "transient", "memory.target_outside_store": "until_fixed", "memory.scope_home_not_directory": "until_fixed", "memory.control_plane_links_unsupported": "until_fixed", "memory.control_plane_strays": "until_fixed", "memory.store_root_sensitive": "until_fixed", "memory.control_plane_split": "until_fixed", "memory.store_absent": "until_fixed", "memory.root_anchor_damaged": "until_fixed", unclassified: "transient", }); export function captureMarkUnpersistedReadingOf(controlDir) { return storeAbsence(controlDir) !== undefined ? { cause: STORE_ABSENT } : { cause: "record_unpersisted" }; } const RECORD_UNPERSISTED = { clause: "the record store did not accept the write", restore: "repair the control plane (or the capture record store)" }; export function captureOptOutUnpersistedWords(reading) { const own = (table, key) => typeof key === "string" && Object.prototype.hasOwnProperty.call(table, key); const cause = reading.cause; const withCases = own(CAPTURE_OPTOUT_UNPERSISTED_RETRY, cause) && cause !== "record_unpersisted" && !own(CLAUSE, cause); if (!own(CAPTURE_OPTOUT_UNPERSISTED_RETRY, cause) || (withCases && !own(OUTSIDE, reading.outsideReason))) { throw new TypeError(`memory.capture_optout_unpersisted: the reading's cause must be one of ${Object.keys(CAPTURE_OPTOUT_UNPERSISTED_RETRY).join(", ")} — the code with cases with one of ${Object.keys(OUTSIDE).join(", ")} (got ${typeof cause === "string" ? JSON.stringify(cause) : cause === undefined ? "no cause" : typeof cause})`); } const words = reading.cause === "record_unpersisted" ? RECORD_UNPERSISTED : mountFailedWordsOf(reading); const why = reading.cause === "record_unpersisted" ? words.clause : `memory did not mount: ${words.clause}`; return { why, retry: CAPTURE_OPTOUT_UNPERSISTED_RETRY[reading.cause] === "until_fixed" ? `a retry fails the same way until this is fixed: ${words.restore}` : `a retry may succeed; if it keeps failing, ${words.restore}` }; } export function captureOptOutUnpersistedDetail(reading) { return { cause: reading.cause, retry: CAPTURE_OPTOUT_UNPERSISTED_RETRY[reading.cause], ...("outsideReason" in reading ? { outsideReason: reading.outsideReason } : {}) }; } export function memoryCaptureOptOutUnpersistedNotice(input) { const { why, retry } = captureOptOutUnpersistedWords(input); const shared = `The memory capture opt-out for this session could not be durably recorded (${why}), so it was NOT honored: no record was made and this session's memory capture is not off.`; const consequence = input.ingress === "declaration" ? ` The session is REFUSED rather than run under an opt-out that would silently stop holding at the next resume — nothing ran, and nothing was captured; ${retry}.` : input.ingress === "flip-verb" ? ` Only the opt-out REQUEST was rejected: this run continues and its memory capture is still ON, so it goes on capturing; ${retry} — or stop the run if the session must not be captured.` : ` What a retry can expect: ${retry}.`; return { code: "memory.capture_optout_unpersisted", message: `${shared}${consequence}`, detail: { sessionId: input.sessionId, ...(input.ingress !== undefined ? { ingress: input.ingress } : {}), ...captureOptOutUnpersistedDetail(input) }, }; }