Skip to content

Commit 8640e42

Browse files
committed
perf(plugins): keep state-db write machinery out of metadata read paths
Metadata-only CLI reads (openclaw plugins list --json and friends) loaded the full Kysely-backed state-db graph (~250 modules) through three read-path fusions. Split them, behavior-neutral (module layout and imports only): - installed-plugin-index-store.ts now only reads/parses; the write, restore, and refresh functions move verbatim to installed-plugin-index-store-write.ts (write transactions are sync per the SQLite commit doctrine, so this is a module split, not a dynamic import). Writer callers and test mocks route to the write module. - refreshPluginRegistry moves out of plugin-registry-snapshot.ts into plugin-registry-refresh.ts, and the plugin-registry.ts barrel no longer re-exports it: ESM evaluates every barrel re-export source, so that one writer re-export dragged the write graph into every barrel consumer on the metadata path. - openclaw-state-db-readonly.ts no longer imports the heavy openclaw-state-db.js module: cache-trio calls go straight to openClawStateDatabaseCache, constants come from openclaw-state-db-contract.js, and registerNodeSqliteKyselyQueryErrorHandler moves into kysely-sync-cache-state.ts (that module's stated charter). Doctor and gateway schema preflights read OPENCLAW_STATE_SCHEMA_VERSION from the contract module instead of value-loading the heavy module; the now-dead wrapper exports are deleted or un-exported. Module-load census on the built CLI (module.registerHooks recorder, isolated HOME): the installed-plugin-index / manifest-registry / store / readonly / snapshot chain no longer imports the openclaw-state-db chunk, and the write module does not load on plugins list --json. kysely itself still loads via the separately owned config-io fusions (io.health-state, legacy-config-migrations -> config-machine-state, sqlite-audit-record-store, cron store) - the remaining named follow-ups from #131181. Named follow-up from #131181 (f49d250).
1 parent 9cb1d2d commit 8640e42

77 files changed

Lines changed: 590 additions & 536 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎scripts/check-kysely-guardrails.mts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ const rawSqliteAllowPathGroups = {
137137
"src/media/store.ts",
138138
"src/plugin-sdk/memory-core-host-engine-storage.ts",
139139
"src/plugins/installed-plugin-index-record-reader.ts",
140+
"src/plugins/installed-plugin-index-store-write.ts",
140141
"src/plugins/installed-plugin-index-store.ts",
141142
"src/plugin-state/plugin-state-store.sqlite.ts",
142143
"src/tasks/task-flow-registry.store.sqlite.ts",

‎src/agents/mcp-oauth-store.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { withTempHome as withBaseTempHome } from "openclaw/plugin-sdk/test-env";
22
import { describe, expect, it } from "vitest";
3+
import { OPENCLAW_STATE_SCHEMA_VERSION } from "../state/openclaw-state-db-contract.js";
34
import {
45
closeOpenClawStateDatabaseForTest,
56
openOpenClawStateDatabase,
6-
OPENCLAW_STATE_SCHEMA_VERSION,
77
} from "../state/openclaw-state-db.js";
88
import { operatorMcpOAuthIdentity } from "./mcp-oauth-identity.js";
99
import {

‎src/audit/execution-owner-lifecycle-receipts.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import type { ExecutionIdentityContextV1 } from "../../packages/gateway-protocol
66
import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js";
77
import type { AdmittedRunContext } from "../agents/admitted-run-context.js";
88
import { bindCronRunReceiptExecution } from "../cron/store/run-receipt-store.js";
9+
import { OPENCLAW_STATE_SCHEMA_VERSION } from "../state/openclaw-state-db-contract.js";
910
import { tableHasColumn, tableExists } from "../state/openclaw-state-db-schema-helpers.js";
1011
import {
11-
OPENCLAW_STATE_SCHEMA_VERSION,
1212
closeOpenClawStateDatabaseForTest,
1313
openOpenClawStateDatabase,
1414
} from "../state/openclaw-state-db.js";

‎src/cli/plugins-cli-test-helpers.ts‎

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type UpdateNpmInstalledHookPacksFn =
3434
type ReadPersistedInstalledPluginIndexFn =
3535
(typeof import("../plugins/installed-plugin-index-store.js"))["readPersistedInstalledPluginIndex"];
3636
type RestorePersistedInstalledPluginIndexIfCurrentFn =
37-
(typeof import("../plugins/installed-plugin-index-store.js"))["restorePersistedInstalledPluginIndexIfCurrent"];
37+
(typeof import("../plugins/installed-plugin-index-store-write.js"))["restorePersistedInstalledPluginIndexIfCurrent"];
3838
type WritePersistedInstalledPluginIndexInstallRecordsFn =
3939
(typeof import("../plugins/installed-plugin-index-records.js"))["writePersistedInstalledPluginIndexInstallRecords"];
4040
type WritePersistedInstalledPluginIndexInstallRecordsWithLeaseFn =
@@ -425,6 +425,14 @@ vi.mock("../plugins/installed-plugin-index-store.js", async (importOriginal) =>
425425
invokeMock<unknown[], unknown>(readPersistedInstalledPluginIndexMock, ...args)) as (
426426
...args: unknown[]
427427
) => unknown,
428+
};
429+
});
430+
431+
vi.mock("../plugins/installed-plugin-index-store-write.js", async (importOriginal) => {
432+
const actual =
433+
await importOriginal<typeof import("../plugins/installed-plugin-index-store-write.js")>();
434+
return {
435+
...actual,
428436
restorePersistedInstalledPluginIndexIfCurrent: ((...args: unknown[]) =>
429437
invokeMock<unknown[], unknown>(
430438
restorePersistedInstalledPluginIndexIfCurrentMock,
@@ -552,18 +560,31 @@ vi.mock("../plugins/plugin-registry.js", () => ({
552560
inspectPluginRegistryMock,
553561
...args,
554562
)) as (typeof import("../plugins/plugin-registry.js"))["inspectPluginRegistry"],
555-
refreshPluginRegistry: ((
556-
...args: Parameters<(typeof import("../plugins/plugin-registry.js"))["refreshPluginRegistry"]>
557-
) =>
558-
invokeMock<
559-
Parameters<(typeof import("../plugins/plugin-registry.js"))["refreshPluginRegistry"]>,
560-
ReturnType<(typeof import("../plugins/plugin-registry.js"))["refreshPluginRegistry"]>
561-
>(
562-
refreshPluginRegistryMock,
563-
...args,
564-
)) as (typeof import("../plugins/plugin-registry.js"))["refreshPluginRegistry"],
565563
}));
566564

565+
vi.mock("../plugins/plugin-registry-refresh.js", async (importOriginal) => {
566+
const actual = await importOriginal<typeof import("../plugins/plugin-registry-refresh.js")>();
567+
return {
568+
...actual,
569+
refreshPluginRegistry: ((
570+
...args: Parameters<
571+
(typeof import("../plugins/plugin-registry-refresh.js"))["refreshPluginRegistry"]
572+
>
573+
) =>
574+
invokeMock<
575+
Parameters<
576+
(typeof import("../plugins/plugin-registry-refresh.js"))["refreshPluginRegistry"]
577+
>,
578+
ReturnType<
579+
(typeof import("../plugins/plugin-registry-refresh.js"))["refreshPluginRegistry"]
580+
>
581+
>(
582+
refreshPluginRegistryMock,
583+
...args,
584+
)) as (typeof import("../plugins/plugin-registry-refresh.js"))["refreshPluginRegistry"],
585+
};
586+
});
587+
567588
vi.mock("../plugins/loader.js", () => ({
568589
clearPluginRegistryLoadCache: ((...args: unknown[]) =>
569590
invokeMock<unknown[], unknown>(clearPluginRegistryLoadCacheMock, ...args)) as (

‎src/cli/plugins-cli.runtime.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,10 @@ export async function runPluginsInstallAction(
340340

341341
/** Inspect or refresh the persisted plugin registry index. */
342342
export async function runPluginsRegistryCommand(opts: PluginRegistryOptions): Promise<void> {
343-
const { inspectPluginRegistry, refreshPluginRegistry } =
344-
await import("../plugins/plugin-registry.js");
343+
const { inspectPluginRegistry } = await import("../plugins/plugin-registry.js");
345344

346345
if (opts.refresh) {
346+
const { refreshPluginRegistry } = await import("../plugins/plugin-registry-refresh.js");
347347
return await withPluginLifecycleLease({}, async () => {
348348
const index = await refreshPluginRegistry({
349349
config: getRuntimeConfig(),

‎src/cli/update-cli.test.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,8 @@ const loadInstalledPluginIndexInstallRecords = vi.fn(
7171
params.config?.plugins?.installs ?? {},
7272
);
7373
const readPersistedInstalledPluginIndex = vi.fn(async () => null);
74-
const restorePersistedInstalledPluginIndex = vi.fn(async () => undefined);
7574
const restorePersistedInstalledPluginIndexIfCurrent = vi.fn<
76-
typeof import("../plugins/installed-plugin-index-store.js").restorePersistedInstalledPluginIndexIfCurrent
75+
typeof import("../plugins/installed-plugin-index-store-write.js").restorePersistedInstalledPluginIndexIfCurrent
7776
>(async () => true);
7877
const writePersistedInstalledPluginIndexInstallRecords = vi.fn(async () => undefined);
7978
const writePersistedInstalledPluginIndexInstallRecordsWithLease = vi.fn(async () => ({
@@ -321,7 +320,14 @@ vi.mock("../plugins/installed-plugin-index-store.js", async (importOriginal) =>
321320
return {
322321
...actual,
323322
readPersistedInstalledPluginIndex,
324-
restorePersistedInstalledPluginIndex,
323+
};
324+
});
325+
326+
vi.mock("../plugins/installed-plugin-index-store-write.js", async (importOriginal) => {
327+
const actual =
328+
await importOriginal<typeof import("../plugins/installed-plugin-index-store-write.js")>();
329+
return {
330+
...actual,
325331
restorePersistedInstalledPluginIndexIfCurrent,
326332
};
327333
});
@@ -1479,7 +1485,6 @@ describe("update-cli", () => {
14791485
vi.resetAllMocks();
14801486
serviceEnabled.mockResolvedValue(true);
14811487
readPersistedInstalledPluginIndex.mockResolvedValue(null);
1482-
restorePersistedInstalledPluginIndex.mockResolvedValue(undefined);
14831488
restorePersistedInstalledPluginIndexIfCurrent.mockResolvedValue(true);
14841489
writePersistedInstalledPluginIndexInstallRecords.mockResolvedValue(undefined);
14851490
writePersistedInstalledPluginIndexInstallRecordsWithLease.mockResolvedValue({

‎src/cli/update-cli/update-command-post-core.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import {
3636
import type { UpdateRunResult } from "../../infra/update-runner.js";
3737
import { getWindowsSystem32ExePath } from "../../infra/windows-install-roots.js";
3838
import { writePersistedInstalledPluginIndexInstallRecordsWithLease } from "../../plugins/installed-plugin-index-records.js";
39-
import { restorePersistedInstalledPluginIndexIfCurrent } from "../../plugins/installed-plugin-index-store.js";
39+
import { restorePersistedInstalledPluginIndexIfCurrent } from "../../plugins/installed-plugin-index-store-write.js";
4040
import { withPluginLifecycleLease } from "../../plugins/plugin-lifecycle-lease.js";
4141
import { runExec } from "../../process/exec.js";
4242
import { defaultRuntime } from "../../runtime.js";

‎src/commands/backup-sqlite.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { createLocalSqliteSnapshotProvider } from "../snapshot/local-repository.
77
import { OPENCLAW_AGENT_SCHEMA_VERSION } from "../state/openclaw-agent-db.js";
88
import { resolveOpenClawAgentSqlitePath } from "../state/openclaw-agent-db.paths.js";
99
import { OPENCLAW_AGENT_SCHEMA_SQL } from "../state/openclaw-agent-schema.js";
10-
import { OPENCLAW_STATE_SCHEMA_VERSION } from "../state/openclaw-state-db.js";
10+
import { OPENCLAW_STATE_SCHEMA_VERSION } from "../state/openclaw-state-db-contract.js";
1111
import { resolveOpenClawStateSqlitePath } from "../state/openclaw-state-db.paths.js";
1212
import { OPENCLAW_STATE_SCHEMA_SQL } from "../state/openclaw-state-schema.js";
1313
import {

‎src/commands/doctor-config-preflight-plugin-index.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { DoctorConfigPreflightPluginSnapshotRead } from "./doctor-config-pr
66

77
const writePersistedInstalledPluginIndexWithLeaseSync = vi.hoisted(() => vi.fn());
88

9-
vi.mock("../plugins/installed-plugin-index-store.js", () => ({
9+
vi.mock("../plugins/installed-plugin-index-store-write.js", () => ({
1010
writePersistedInstalledPluginIndexWithLeaseSync,
1111
}));
1212

‎src/commands/doctor-config-preflight-plugin-index.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import { createLazyRuntimeModule } from "../shared/lazy-runtime.js";
1010
import { addDoctorLegacyIssues } from "./doctor/shared/legacy-config-issues.js";
1111
import { completeDoctorPluginMetadataSnapshot } from "./doctor/shared/plugin-metadata-snapshot-scope.js";
1212

13-
const loadInstalledPluginIndexStore = createLazyRuntimeModule(
14-
() => import("../plugins/installed-plugin-index-store.js"),
13+
const loadInstalledPluginIndexStoreWrite = createLazyRuntimeModule(
14+
() => import("../plugins/installed-plugin-index-store-write.js"),
1515
);
1616

1717
export type DoctorConfigPreflightPluginSnapshotRead = {
@@ -89,7 +89,7 @@ export async function persistRefreshedPluginIndex(params: {
8989
}
9090
const { writePersistedInstalledPluginIndexWithLeaseSync } = await params.measure(
9191
"plugin-index-store-import",
92-
loadInstalledPluginIndexStore,
92+
loadInstalledPluginIndexStoreWrite,
9393
);
9494
// The checkpoint certifies the persisted inventory, not a process-local replacement.
9595
// Write the exact derived index first, then prove a fresh reader can reuse it.

0 commit comments

Comments
 (0)