openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
172 lines (171 loc) • 7.05 kB
JavaScript
import { a as asOptionalRecord } from "../../record-coerce-CVPuDqcc.mjs";
import "../../string-coerce-runtime-BwAZZDf5.mjs";
import { c as projectCrabboxLegacyWarmLeases, n as crabboxLegacyWarmImageCaptureSelector } from "../../crabbox-worker-warm-image-records-DeuyTta3.mjs";
//#region extensions/crabbox/doctor-contract-api.ts
const imageFields = /* @__PURE__ */ new Set([
"checkpointId",
"kind",
"state",
"createdAtMs",
"lastUsedAtMs",
"baseCommit",
"operation",
"runtimeIdentity"
]);
const captureFields = /* @__PURE__ */ new Set([
"type",
"id",
"startedAtMs",
"leaseId",
"provider",
"phase"
]);
const retirementFields = /* @__PURE__ */ new Set(["type", "checkpointId"]);
const profileFields = /* @__PURE__ */ new Set([
"version",
"projectKey",
"image",
"allocations",
"operation"
]);
const allocationFields = /* @__PURE__ */ new Set([
"choice",
"machineClass",
"os",
"phase",
"baseCommit",
"runtimeIdentity"
]);
const nonempty = (e) => typeof e === "string" && e.trim().length > 0;
const timestamp = (e) => typeof e === "number" && Number.isFinite(e);
function isLegacyOperation(e) {
if (e === void 0) return true;
const t = asOptionalRecord(e);
if (!t) return false;
if (t.type === "retire") return Object.keys(t).every((e) => retirementFields.has(e)) && nonempty(t.checkpointId);
return t.type === "capture" && Object.keys(t).every((e) => captureFields.has(e)) && nonempty(t.id) && timestamp(t.startedAtMs) && (t.leaseId === void 0 || nonempty(t.leaseId)) && (t.provider === void 0 || nonempty(t.provider)) && (t.phase === "scrubbing" || t.phase === "creating" || t.phase === "uncertain");
}
function isLegacyImage(e) {
const t = asOptionalRecord(e);
return Boolean(t && Object.keys(t).every((e) => imageFields.has(e)) && typeof t.checkpointId === "string" && typeof t.kind === "string" && (t.state === "pending" || t.state === "available") && timestamp(t.createdAtMs) && timestamp(t.lastUsedAtMs) && (t.baseCommit === void 0 || nonempty(t.baseCommit)) && (t.checkpointId ? nonempty(t.checkpointId) && nonempty(t.kind) : t.kind === "" && t.state === "pending") && isLegacyOperation(t.operation));
}
function isLegacyAllocation(e) {
const t = asOptionalRecord(e);
const n = asOptionalRecord(t?.choice);
return Boolean(t && n && Object.keys(t).every((e) => allocationFields.has(e)) && nonempty(t.machineClass) && (t.os === void 0 || t.os === "linux" || t.os === "windows/wsl2") && (t.phase === "pending" || t.phase === "prepared" || t.phase === "enrolled") && (t.baseCommit === void 0 || nonempty(t.baseCommit)) && (n.kind === "cold" ? Object.keys(n).every((e) => e === "kind") : n.kind === "checkpoint" && Object.keys(n).every((e) => e === "kind" || e === "checkpointId") && nonempty(n.checkpointId)));
}
function isLegacyProfile(e) {
const t = asOptionalRecord(e);
const n = asOptionalRecord(t?.allocations);
return Boolean(t?.version === 2 && Object.keys(t).every((e) => profileFields.has(e)) && (t.projectKey === void 0 || nonempty(t.projectKey)) && (t.image === void 0 || isLegacyImage(t.image) && t.image.operation === void 0) && n && Object.values(n).every(isLegacyAllocation) && isLegacyOperation(t.operation));
}
function migrateImage({ lastUsedAtMs: e, operation: t, ...n }) {
return {
...n,
lastDemandAtMs: null,
preparationKey: null,
cacheKey: null,
purpose: null
};
}
async function listLegacyWarmLeases(e) {
return projectCrabboxLegacyWarmLeases(await e.openPluginStateKeyedStore({
namespace: "warm-leases",
maxEntries: 256,
overflowPolicy: "evict-oldest"
}).entries());
}
const stateMigrations = [{
id: "crabbox-warm-profile-v3",
label: "Crabbox warm profiles",
doctorOnly: true,
collectBackupResources: () => [],
async detectLegacyState({ context: e }) {
const t = (await e.openPluginStateKeyedStore({
namespace: "warm-images",
maxEntries: 128,
overflowPolicy: "reject-new"
}).entries()).filter(({ value: e }) => asOptionalRecord(e)?.version !== 3).length;
const n = await listLegacyWarmLeases(e);
return t || n.length ? { preview: [...t ? [`- ${t} Crabbox warm-image row(s) require preparation/cache-record migration or manual repair.`] : [], ...n.map((e) => `- Legacy Crabbox lease ${e.leaseId} requires provider cleanup acknowledgement: openclaw crabbox warm-images --recover ${e.selector} --acknowledge-provider-cleanup`)] } : null;
},
async migrateLegacyState({ context: e }) {
const t = [];
const n = [];
const r = e.openPluginStateKeyedStore({
namespace: "warm-images",
maxEntries: 128,
overflowPolicy: "reject-new"
});
for (const { key: e, value: i } of await r.entries()) {
if (asOptionalRecord(i)?.version === 3) continue;
let a;
if (isLegacyProfile(i)) {
const { image: e, allocations: t, ...n } = i;
a = {
...n,
version: 3,
...e ? { image: migrateImage(e) } : {},
allocations: Object.fromEntries(Object.entries(t).map(([e, t]) => [e, {
...t,
preparationKey: null,
cacheKey: null,
purpose: null,
demandAtMs: null,
imageGeneration: null
}]))
};
} else if (isLegacyImage(i)) {
const { operation: t } = i;
a = {
version: 3,
...i.checkpointId ? { image: migrateImage(i) } : {},
allocations: {},
...t ? { operation: t } : !i.checkpointId ? { operation: {
type: "capture",
id: crabboxLegacyWarmImageCaptureSelector(e, i),
startedAtMs: i.createdAtMs,
phase: "uncertain"
} } : {}
};
} else {
n.push(`Crabbox warm profile ${e} has an unsupported record; left it unchanged for manual repair.`);
continue;
}
try {
let o = false;
if (r.observe && r.compareAndApply) {
const t = JSON.stringify(i);
let n = await r.observe(e);
while (JSON.stringify(n.value) === t) {
const t = await r.compareAndApply(e, n.comparison, {
operation: "update",
action: "set",
value: a
});
if (t.status !== "conflict") {
o = t.status === "applied";
break;
}
n = t.current;
}
}
if (o) t.push(`Migrated Crabbox warm profile ${e}, preserving resource obligations without inventing preparation, cache identity, purpose or demand.`);
else n.push(`Crabbox warm profile ${e} changed or atomic update is unavailable; left it unchanged.`);
} catch (t) {
n.push(`Failed migrating Crabbox warm profile ${e}; left it unchanged: ${String(t)}`);
}
}
const i = await listLegacyWarmLeases(e);
if (i.length) {
n.push(`${i.length} legacy Crabbox lease row(s) still block warm-profile admission; their original cold/checkpoint choices are unknown.`);
for (const e of i) n.push(`Resolve lease ${e.leaseId} through its original Gateway or provider, stop the original Gateway/capture processes, and confirm provider cleanup before running: openclaw crabbox warm-images --recover ${e.selector} --acknowledge-provider-cleanup. Then rerun openclaw doctor --fix. The row was not changed.`);
}
return {
changes: t,
warnings: n
};
}
}];
//#endregion
export { stateMigrations };