UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

104 lines (103 loc) • 5.62 kB
import { codedRefusal } from "../core/thrown-value.js"; import { describeRejectedValue } from "../core/tool-search-setting.js"; import { isLaneKey, laneKeyOf, laneOfIntent, WAKEUP_LABEL } from "./scheduler-tools.js"; const LIFETIMES = [undefined, "durable", "session"]; const MODES = [undefined, "task", "session-wakeup"]; const LANES = [undefined, "session", "durable"]; function rowsOf(store, direction, wanted, want) { const held = store; if (typeof held !== "object" || held === null || !Array.isArray(held.rows)) { throw codedRefusal("config.invalid_argument", `the scheduler store handed to the ${direction} is ${describeRejectedValue(held)}, not { scopeKeys?, rows: [...] } — the ${direction} reads the whole store at once.`); } const keys = held.scopeKeys; if (!wanted(keys)) throw codedRefusal("config.invalid_argument", `the scheduler store declares scopeKeys ${describeRejectedValue(keys)}; the ${direction} runs on a store that declares ${want} — it runs once, and a key's head does not say which form it is.`); return held.rows; } function storedLaneOf(row) { if (!LIFETIMES.includes(row.lifetime)) return { fault: `its lifetime is ${describeRejectedValue(row.lifetime)}, not "durable", "session" or absent` }; if (!MODES.includes(row.mode)) return { fault: `its mode is ${describeRejectedValue(row.mode)}, not "task", "session-wakeup" or absent` }; const lane = laneOfIntent(row); return lane === "durable" && row.mode === undefined && row.label === WAKEUP_LABEL ? "unknown" : lane; } export function migrateSchedulerStore(store) { const rows = rowsOf(store, "move", (keys) => keys === undefined || keys === "bare", 'none, or "bare"'); const placed = []; const refused = []; const refuse = (row, code, reason) => void refused.push({ row, code, reason }); for (const row of rows) { const key = row?.scope; if (typeof key !== "string" || key === "") { refuse(row, "row_invalid", `the row's key is ${describeRejectedValue(key)}, not a non-empty string — there is no owner to file it under.`); continue; } const stored = storedLaneOf(row); if (typeof stored === "object") { refuse(row, "row_invalid", `${stored.fault} — its lane cannot be read.`); continue; } if (!LANES.includes(row.lane)) { refuse(row, "row_invalid", `its stated lane is ${describeRejectedValue(row.lane)}, not "session", "durable" or absent.`); continue; } const stated = row.lane; if (stated !== undefined && stored !== "unknown" && stated !== stored) { refuse(row, "lane_contradicted", `its stated lane is "${stated}", but its stored lifetime and mode file it on the ${stored} lane.`); continue; } const lane = stated ?? stored; if (lane === "unknown") { refuse(row, "lane_unknown", `it carries ${row.lifetime === undefined ? "no lifetime" : `lifetime "${String(row.lifetime)}"`} and no mode, and the wakeup's label "${WAKEUP_LABEL}" — a row both lanes may have written: a session wakeup on a store that kept no mode, or a job given that label. State its lane.`); continue; } if (lane === "durable") { placed.push({ row, scope: laneKeyOf("durable", key), from: key }); continue; } const destination = row.sessionId; if (typeof destination !== "string" || destination === "") { refuse(row, "destination_unknown", `it is a session-lane row whose session (sessionId) is ${describeRejectedValue(destination)}, not a non-empty string — the session it fires into is the owner of its key.`); continue; } placed.push({ row, scope: laneKeyOf("session", destination), from: key }); } const moved = unshared(placed, refused, (shared, froms) => ({ code: "lane_key_shared", reason: `its lane key ${JSON.stringify(shared)} would be shared by rows filed under ${froms.map((k) => JSON.stringify(k)).join(" and ")}: the move does not merge two owners' rows into one lane.`, })); return { moved, refused }; } function unshared(placed, refused, why) { const fromsOf = new Map(); for (const p of placed) fromsOf.set(p.scope, (fromsOf.get(p.scope) ?? new Set()).add(p.from)); const moved = []; for (const p of placed) { const froms = fromsOf.get(p.scope); if (froms.size === 1) moved.push({ row: p.row, scope: p.scope }); else refused.push({ row: p.row, ...why(p.scope, [...froms]) }); } return moved; } export function rollbackSchedulerStore(store) { const rows = rowsOf(store, "rollback", (keys) => keys === "lane", '"lane"'); const placed = []; const refused = []; for (const row of rows) { const key = row?.scope; const lane = ["session", "durable"].find((l) => isLaneKey(l, key)); if (lane === undefined) refused.push({ row, code: "row_invalid", reason: `the row's key is ${describeRejectedValue(key)}, not a lane's name, \`:\` and a non-empty owner.` }); else placed.push({ row, scope: key.slice(lane.length + 1), from: key }); } const moved = unshared(placed, refused, (_shared, froms) => ({ code: "bare_key_shared", reason: `its bare key is shared by ${froms.map((k) => JSON.stringify(k)).join(" and ")}: an older release lists and stops them as one lane's rows.`, })); return { moved, refused }; }