mirror of
https://github.com/moltbot/moltbot.git
synced 2026-05-21 21:56:46 +00:00
refactor: remove runtime commitments kv migration
This commit is contained in:
@@ -3,7 +3,6 @@ import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { openOpenClawStateDatabase } from "../state/openclaw-state-db.js";
|
||||
import { writeOpenClawStateKvJson } from "../state/openclaw-state-kv.js";
|
||||
import {
|
||||
listCommitments,
|
||||
listDueCommitmentsForSession,
|
||||
@@ -129,9 +128,9 @@ describe("commitment store delivery selection", () => {
|
||||
expect(store.commitments[0]?.updatedAtMs).toBe(nowMs);
|
||||
});
|
||||
|
||||
it("rewrites legacy source text fields when due commitments are listed", async () => {
|
||||
it("rewrites legacy source text fields when commitments are saved", async () => {
|
||||
await useTempStateDir();
|
||||
writeOpenClawStateKvJson("commitments", "store", {
|
||||
await saveCommitmentStore(undefined, {
|
||||
version: 1,
|
||||
commitments: [commitment()],
|
||||
});
|
||||
|
||||
@@ -11,7 +11,6 @@ import {
|
||||
type OpenClawStateDatabaseOptions,
|
||||
} from "../state/openclaw-state-db.js";
|
||||
import { resolveOpenClawStateSqlitePath } from "../state/openclaw-state-db.paths.js";
|
||||
import { deleteOpenClawStateKvJson, readOpenClawStateKvJson } from "../state/openclaw-state-kv.js";
|
||||
import {
|
||||
DEFAULT_COMMITMENT_EXPIRE_AFTER_HOURS,
|
||||
DEFAULT_COMMITMENT_MAX_PER_HEARTBEAT,
|
||||
@@ -28,8 +27,6 @@ import type {
|
||||
|
||||
const STORE_VERSION = 1 as const;
|
||||
const ROLLING_DAY_MS = 24 * 60 * 60 * 1000;
|
||||
const COMMITMENT_STORE_SCOPE = "commitments";
|
||||
const COMMITMENT_STORE_KEY = "store";
|
||||
const LEGACY_COMMITMENT_STORE_RELATIVE_PATH = path.join("commitments", "commitments.json");
|
||||
|
||||
type LoadedCommitmentStore = {
|
||||
@@ -157,29 +154,6 @@ async function loadCommitmentStoreFromFile(resolved: string): Promise<LoadedComm
|
||||
|
||||
function loadCommitmentStoreFromSqlite(
|
||||
env: NodeJS.ProcessEnv = process.env,
|
||||
): LoadedCommitmentStore {
|
||||
const database = openOpenClawStateDatabase(sqliteOptionsForEnv(env));
|
||||
const db = getNodeSqliteKysely<CommitmentsDatabase>(database.db);
|
||||
const rows = executeSqliteQuerySync<CommitmentRow>(
|
||||
database.db,
|
||||
db
|
||||
.selectFrom("commitments")
|
||||
.select(["id", "record_json"])
|
||||
.orderBy("due_earliest_ms", "asc")
|
||||
.orderBy("id", "asc"),
|
||||
).rows;
|
||||
if (rows.length === 0) {
|
||||
migrateLegacyCommitmentsKvToRows(env);
|
||||
return loadCommitmentStoreFromSqliteRows(env);
|
||||
}
|
||||
return coerceCommitmentStore({
|
||||
version: STORE_VERSION,
|
||||
commitments: rows.map((row) => parseCommitmentRow(row)),
|
||||
});
|
||||
}
|
||||
|
||||
function loadCommitmentStoreFromSqliteRows(
|
||||
env: NodeJS.ProcessEnv = process.env,
|
||||
): LoadedCommitmentStore {
|
||||
const database = openOpenClawStateDatabase(sqliteOptionsForEnv(env));
|
||||
const db = getNodeSqliteKysely<CommitmentsDatabase>(database.db);
|
||||
@@ -205,20 +179,6 @@ function parseCommitmentRow(row: CommitmentRow): unknown {
|
||||
}
|
||||
}
|
||||
|
||||
function migrateLegacyCommitmentsKvToRows(env: NodeJS.ProcessEnv): void {
|
||||
const legacy = readOpenClawStateKvJson(
|
||||
COMMITMENT_STORE_SCOPE,
|
||||
COMMITMENT_STORE_KEY,
|
||||
sqliteOptionsForEnv(env),
|
||||
);
|
||||
if (legacy === undefined) {
|
||||
return;
|
||||
}
|
||||
const { store } = coerceCommitmentStore(legacy);
|
||||
replaceCommitmentRows(store, env);
|
||||
deleteOpenClawStateKvJson(COMMITMENT_STORE_SCOPE, COMMITMENT_STORE_KEY, sqliteOptionsForEnv(env));
|
||||
}
|
||||
|
||||
function loadCommitmentStoreInternal(): LoadedCommitmentStore {
|
||||
return loadCommitmentStoreFromSqlite();
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import { HEARTBEAT_TOKEN } from "../auto-reply/tokens.js";
|
||||
import { loadCommitmentStore, saveCommitmentStore } from "../commitments/store.js";
|
||||
import type { CommitmentRecord, CommitmentStoreFile } from "../commitments/types.js";
|
||||
import type { OpenClawConfig } from "../config/config.js";
|
||||
import { writeOpenClawStateKvJson } from "../state/openclaw-state-kv.js";
|
||||
import {
|
||||
runHeartbeatOnce,
|
||||
setHeartbeatsEnabled,
|
||||
@@ -67,7 +66,6 @@ describe("runHeartbeatOnce commitments", () => {
|
||||
target?: "last" | "none";
|
||||
sourceUserText?: string;
|
||||
sourceAssistantText?: string;
|
||||
legacyRawSourceText?: boolean;
|
||||
visibleReplies?: "automatic" | "message_tool";
|
||||
}) {
|
||||
return await withTempHeartbeatSandbox(async ({ tmpDir, agentId, replySpy }) => {
|
||||
@@ -105,11 +103,7 @@ describe("runHeartbeatOnce commitments", () => {
|
||||
}),
|
||||
],
|
||||
};
|
||||
if (params?.legacyRawSourceText) {
|
||||
writeOpenClawStateKvJson("commitments", "store", storePayload);
|
||||
} else {
|
||||
await saveCommitmentStore(undefined, storePayload);
|
||||
}
|
||||
await saveCommitmentStore(undefined, storePayload);
|
||||
|
||||
const sendTelegram = vi.fn().mockResolvedValue({
|
||||
messageId: "m1",
|
||||
@@ -418,7 +412,6 @@ describe("runHeartbeatOnce commitments", () => {
|
||||
const { result, sendTelegram, store } = await setupCommitmentCase({
|
||||
sourceUserText: maliciousUserText,
|
||||
sourceAssistantText: maliciousAssistantText,
|
||||
legacyRawSourceText: true,
|
||||
});
|
||||
|
||||
expect(result.status).toBe("ran");
|
||||
|
||||
Reference in New Issue
Block a user