mirror of
https://github.com/Youzini-afk/ST-Bionic-Memory-Ecology.git
synced 2026-06-14 02:40:45 +08:00
chore: remove abandoned hard-cut v3 namespace cluster
This commit is contained in:
@@ -1,140 +0,0 @@
|
||||
// ST-BME v3 GraphStore contract and pure router shell.
|
||||
//
|
||||
// Phase 6 only defines/validates the contract and route plans. Live adapters are
|
||||
// ported in Phase 7 so durable routing is not switched accidentally.
|
||||
|
||||
export const GRAPH_STORE_CONTRACT_VERSION = 3;
|
||||
|
||||
export const GRAPH_STORE_KINDS = Object.freeze({
|
||||
AUTHORITY: "authority",
|
||||
OPFS: "opfs",
|
||||
INDEXEDDB: "indexeddb",
|
||||
LUKER_CHAT_STATE: "luker-chat-state",
|
||||
NONE: "none",
|
||||
});
|
||||
|
||||
export const GRAPH_STORE_REQUIRED_METHODS = Object.freeze([
|
||||
"open",
|
||||
"close",
|
||||
"getMeta",
|
||||
"patchMeta",
|
||||
"commitDelta",
|
||||
"exportSnapshot",
|
||||
"exportSnapshotProbe",
|
||||
"importSnapshot",
|
||||
]);
|
||||
|
||||
export const GRAPH_STORE_OPTIONAL_METHODS = Object.freeze([
|
||||
"readHead",
|
||||
"writeHead",
|
||||
"readCommitMarker",
|
||||
"writeCommitMarker",
|
||||
"isEmpty",
|
||||
"deleteAll",
|
||||
]);
|
||||
|
||||
function normalizeStoreKind(value = "") {
|
||||
const kind = String(value || "").trim().toLowerCase();
|
||||
if (Object.values(GRAPH_STORE_KINDS).includes(kind)) return kind;
|
||||
return GRAPH_STORE_KINDS.NONE;
|
||||
}
|
||||
|
||||
function methodExists(store = null, method = "") {
|
||||
return store && typeof store[method] === "function";
|
||||
}
|
||||
|
||||
export function inspectGraphStoreContract(store = null, options = {}) {
|
||||
const requiredMethods = Array.isArray(options.requiredMethods)
|
||||
? options.requiredMethods
|
||||
: GRAPH_STORE_REQUIRED_METHODS;
|
||||
const optionalMethods = Array.isArray(options.optionalMethods)
|
||||
? options.optionalMethods
|
||||
: GRAPH_STORE_OPTIONAL_METHODS;
|
||||
const missingMethods = requiredMethods.filter((method) => !methodExists(store, method));
|
||||
const supportedOptionalMethods = optionalMethods.filter((method) => methodExists(store, method));
|
||||
return {
|
||||
contractVersion: GRAPH_STORE_CONTRACT_VERSION,
|
||||
valid: missingMethods.length === 0,
|
||||
storeKind: normalizeStoreKind(store?.storeKind || store?.kind),
|
||||
storeMode: String(store?.storeMode || store?.mode || ""),
|
||||
missingMethods,
|
||||
supportedOptionalMethods,
|
||||
};
|
||||
}
|
||||
|
||||
export function assertGraphStoreContract(store = null, options = {}) {
|
||||
const inspection = inspectGraphStoreContract(store, options);
|
||||
if (!inspection.valid) {
|
||||
const error = new Error(`graph-store-contract-invalid:${inspection.missingMethods.join(",")}`);
|
||||
error.code = "graph_store_contract_invalid";
|
||||
error.contract = inspection;
|
||||
throw error;
|
||||
}
|
||||
return inspection;
|
||||
}
|
||||
|
||||
function normalizeBoolean(value) {
|
||||
return value === true;
|
||||
}
|
||||
|
||||
function normalizePreference(value = "") {
|
||||
const normalized = String(value || "").trim().toLowerCase();
|
||||
if (normalized === "authority-sql") return GRAPH_STORE_KINDS.AUTHORITY;
|
||||
if (normalized === "opfs-primary" || normalized === "opfs-shadow") return GRAPH_STORE_KINDS.OPFS;
|
||||
if (normalized === "indexeddb") return GRAPH_STORE_KINDS.INDEXEDDB;
|
||||
if (normalized === "luker-chat-state") return GRAPH_STORE_KINDS.LUKER_CHAT_STATE;
|
||||
return "auto";
|
||||
}
|
||||
|
||||
function pushUniqueRoute(routes, kind, reason = "") {
|
||||
const normalizedKind = normalizeStoreKind(kind);
|
||||
if (!normalizedKind || normalizedKind === GRAPH_STORE_KINDS.NONE) return;
|
||||
if (routes.some((route) => route.kind === normalizedKind)) return;
|
||||
routes.push({ kind: normalizedKind, reason: String(reason || normalizedKind) });
|
||||
}
|
||||
|
||||
export function planGraphStoreRoute(input = {}) {
|
||||
const preference = normalizePreference(input.preference || input.primaryStorageTier || input.localStoreMode);
|
||||
const capabilities = input.capabilities && typeof input.capabilities === "object" ? input.capabilities : {};
|
||||
const environment = input.environment && typeof input.environment === "object" ? input.environment : {};
|
||||
const hardCutNamespace = input.hardCutNamespace && typeof input.hardCutNamespace === "object"
|
||||
? input.hardCutNamespace
|
||||
: null;
|
||||
const routes = [];
|
||||
|
||||
const authorityReady = normalizeBoolean(capabilities.authoritySqlReady || capabilities.storagePrimaryReady);
|
||||
const opfsReady = normalizeBoolean(capabilities.opfsReady || capabilities.opfsAvailable);
|
||||
const indexedDbReady =
|
||||
normalizeBoolean(capabilities.indexedDbReady) || normalizeBoolean(capabilities.indexedDbAvailable);
|
||||
const lukerReady = normalizeBoolean(environment.lukerChatStateReady || capabilities.lukerChatStateReady);
|
||||
|
||||
if (preference === GRAPH_STORE_KINDS.AUTHORITY && authorityReady) {
|
||||
pushUniqueRoute(routes, GRAPH_STORE_KINDS.AUTHORITY, "preferred-authority-sql");
|
||||
}
|
||||
if (preference === GRAPH_STORE_KINDS.OPFS && opfsReady) {
|
||||
pushUniqueRoute(routes, GRAPH_STORE_KINDS.OPFS, "preferred-opfs");
|
||||
}
|
||||
if (preference === GRAPH_STORE_KINDS.INDEXEDDB && indexedDbReady) {
|
||||
pushUniqueRoute(routes, GRAPH_STORE_KINDS.INDEXEDDB, "preferred-indexeddb");
|
||||
}
|
||||
if (preference === GRAPH_STORE_KINDS.LUKER_CHAT_STATE && lukerReady) {
|
||||
pushUniqueRoute(routes, GRAPH_STORE_KINDS.LUKER_CHAT_STATE, "preferred-luker-chat-state");
|
||||
}
|
||||
|
||||
if (authorityReady) pushUniqueRoute(routes, GRAPH_STORE_KINDS.AUTHORITY, "authority-sql-ready");
|
||||
if (opfsReady) pushUniqueRoute(routes, GRAPH_STORE_KINDS.OPFS, "opfs-ready");
|
||||
if (indexedDbReady) pushUniqueRoute(routes, GRAPH_STORE_KINDS.INDEXEDDB, "indexeddb-ready");
|
||||
if (lukerReady) pushUniqueRoute(routes, GRAPH_STORE_KINDS.LUKER_CHAT_STATE, "luker-chat-state-ready");
|
||||
|
||||
return {
|
||||
contractVersion: GRAPH_STORE_CONTRACT_VERSION,
|
||||
hardCut: true,
|
||||
hotPathReadsLegacy: false,
|
||||
namespace: hardCutNamespace,
|
||||
primary: routes[0]?.kind || GRAPH_STORE_KINDS.NONE,
|
||||
fallback: routes.slice(1).map((route) => route.kind),
|
||||
routes,
|
||||
blocked: routes.length === 0,
|
||||
reason: routes.length ? routes[0].reason : "no-graph-store-route-ready",
|
||||
};
|
||||
}
|
||||
@@ -1,172 +0,0 @@
|
||||
// ST-BME v3 GraphStore adapter wrappers.
|
||||
//
|
||||
// These wrappers add v3 head/marker sidecar methods to existing stores without
|
||||
// changing their legacy load/save behavior. Physical namespace cutover is handled
|
||||
// by dedicated constructors/routes later.
|
||||
|
||||
import {
|
||||
GRAPH_V3_COMMIT_MARKER_KEY,
|
||||
GRAPH_V3_HEAD_KEY,
|
||||
} from "../graph/graph-v3-namespace.js";
|
||||
import {
|
||||
normalizeCommitMarkerV3,
|
||||
normalizeGraphHead,
|
||||
} from "../graph/graph-head.js";
|
||||
import {
|
||||
readGraphChatStateNamespaces,
|
||||
writeGraphChatStatePayload,
|
||||
} from "../graph/graph-persistence.js";
|
||||
import { assertGraphStoreContract } from "./graph-store-contract.js";
|
||||
|
||||
const GRAPH_STORE_V3_WRAPPED = Symbol.for("st-bme.graph-store-v3-wrapped");
|
||||
|
||||
function bindStoreMethod(store = null, method = "") {
|
||||
const value = store?.[method];
|
||||
return typeof value === "function" ? value.bind(store) : value;
|
||||
}
|
||||
|
||||
export function isGraphStoreV3Wrapped(store = null) {
|
||||
return Boolean(store?.[GRAPH_STORE_V3_WRAPPED]);
|
||||
}
|
||||
|
||||
export function wrapDbLikeGraphStoreV3(store = null) {
|
||||
assertGraphStoreContract(store);
|
||||
if (isGraphStoreV3Wrapped(store)) return store;
|
||||
|
||||
const wrapper = Object.create(store);
|
||||
Object.defineProperty(wrapper, GRAPH_STORE_V3_WRAPPED, {
|
||||
value: true,
|
||||
enumerable: false,
|
||||
});
|
||||
|
||||
for (const key of Reflect.ownKeys(store)) {
|
||||
if (key === GRAPH_STORE_V3_WRAPPED) continue;
|
||||
const descriptor = Object.getOwnPropertyDescriptor(store, key);
|
||||
if (descriptor) Object.defineProperty(wrapper, key, descriptor);
|
||||
}
|
||||
|
||||
for (const method of [
|
||||
"open",
|
||||
"close",
|
||||
"getMeta",
|
||||
"setMeta",
|
||||
"patchMeta",
|
||||
"commitDelta",
|
||||
"exportSnapshot",
|
||||
"exportSnapshotProbe",
|
||||
"importSnapshot",
|
||||
"isEmpty",
|
||||
"clearAll",
|
||||
]) {
|
||||
if (typeof store[method] === "function") {
|
||||
wrapper[method] = bindStoreMethod(store, method);
|
||||
}
|
||||
}
|
||||
|
||||
wrapper.readHead = async ({ fallback = null } = {}) => {
|
||||
const raw = await store.getMeta(GRAPH_V3_HEAD_KEY, null);
|
||||
return raw == null ? fallback : normalizeGraphHead(raw, fallback || {});
|
||||
};
|
||||
|
||||
wrapper.writeHead = async (head = null, { fallback = null } = {}) => {
|
||||
const normalized = normalizeGraphHead(head, fallback || {});
|
||||
await store.patchMeta({ [GRAPH_V3_HEAD_KEY]: normalized });
|
||||
return normalized;
|
||||
};
|
||||
|
||||
wrapper.readCommitMarker = async ({ fallback = null } = {}) => {
|
||||
const raw = await store.getMeta(GRAPH_V3_COMMIT_MARKER_KEY, null);
|
||||
return normalizeCommitMarkerV3(raw) || fallback;
|
||||
};
|
||||
|
||||
wrapper.writeCommitMarker = async (marker = null) => {
|
||||
const normalized = normalizeCommitMarkerV3(marker);
|
||||
if (!normalized) {
|
||||
const error = new Error("graph-store-v3-commit-marker-invalid");
|
||||
error.code = "graph_store_v3_commit_marker_invalid";
|
||||
throw error;
|
||||
}
|
||||
await store.patchMeta({ [GRAPH_V3_COMMIT_MARKER_KEY]: normalized });
|
||||
return normalized;
|
||||
};
|
||||
|
||||
wrapper.deleteAll = async (...args) => {
|
||||
if (typeof store.clearAll !== "function") {
|
||||
const error = new Error("graph-store-v3-delete-all-unavailable");
|
||||
error.code = "graph_store_v3_delete_all_unavailable";
|
||||
throw error;
|
||||
}
|
||||
return store.clearAll(...args);
|
||||
};
|
||||
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
export function createLukerChatStateGraphStoreV3({
|
||||
context = null,
|
||||
chatStateTarget = null,
|
||||
storeKind = "luker-chat-state",
|
||||
storeMode = "luker-chat-state-v3",
|
||||
} = {}) {
|
||||
async function readNamespace(namespace = "", fallback = null) {
|
||||
const payloads = await readGraphChatStateNamespaces(context, [namespace], {
|
||||
target: chatStateTarget,
|
||||
});
|
||||
return payloads.get(namespace) ?? fallback;
|
||||
}
|
||||
|
||||
async function writeNamespace(namespace = "", payload = null) {
|
||||
const result = await writeGraphChatStatePayload(context, namespace, payload, {
|
||||
target: chatStateTarget,
|
||||
});
|
||||
if (result?.ok !== true) {
|
||||
const error = new Error(result?.reason || "luker-graph-store-v3-write-failed");
|
||||
error.code = "luker_graph_store_v3_write_failed";
|
||||
error.result = result;
|
||||
throw error;
|
||||
}
|
||||
return payload;
|
||||
}
|
||||
|
||||
return {
|
||||
storeKind,
|
||||
storeMode,
|
||||
async open() {
|
||||
return this;
|
||||
},
|
||||
async close() {},
|
||||
async getMeta(key = "", fallbackValue = null) {
|
||||
return readNamespace(String(key || ""), fallbackValue);
|
||||
},
|
||||
async patchMeta(record = {}) {
|
||||
const entries = Object.entries(record && typeof record === "object" ? record : {});
|
||||
for (const [key, value] of entries) {
|
||||
await writeNamespace(key, value);
|
||||
}
|
||||
return record;
|
||||
},
|
||||
async readHead({ fallback = null } = {}) {
|
||||
const raw = await readNamespace(GRAPH_V3_HEAD_KEY, null);
|
||||
return raw == null ? fallback : normalizeGraphHead(raw, fallback || {});
|
||||
},
|
||||
async writeHead(head = null, { fallback = null } = {}) {
|
||||
const normalized = normalizeGraphHead(head, fallback || {});
|
||||
await writeNamespace(GRAPH_V3_HEAD_KEY, normalized);
|
||||
return normalized;
|
||||
},
|
||||
async readCommitMarker({ fallback = null } = {}) {
|
||||
const raw = await readNamespace(GRAPH_V3_COMMIT_MARKER_KEY, null);
|
||||
return normalizeCommitMarkerV3(raw) || fallback;
|
||||
},
|
||||
async writeCommitMarker(marker = null) {
|
||||
const normalized = normalizeCommitMarkerV3(marker);
|
||||
if (!normalized) {
|
||||
const error = new Error("luker-graph-store-v3-commit-marker-invalid");
|
||||
error.code = "luker_graph_store_v3_commit_marker_invalid";
|
||||
throw error;
|
||||
}
|
||||
await writeNamespace(GRAPH_V3_COMMIT_MARKER_KEY, normalized);
|
||||
return normalized;
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user