Skip to content

Commit ffa6820

Browse files
committed
fix(plugins): preserve newer schema errors
1 parent bb1ce58 commit ffa6820

7 files changed

Lines changed: 70 additions & 6 deletions

‎src/infra/sqlite-user-version.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ type SqliteUserVersionReader = {
77
prepare: (sql: string) => { get: () => unknown };
88
};
99

10+
const SQLITE_SCHEMA_VERSION_ERROR_NAME = "SqliteSchemaVersionError";
11+
12+
export function isSqliteSchemaVersionError(error: unknown): error is Error {
13+
return error instanceof Error && error.name === SQLITE_SCHEMA_VERSION_ERROR_NAME;
14+
}
15+
1016
export function readSqliteUserVersion(db: SqliteUserVersionReader): number {
1117
const row = db.prepare("PRAGMA user_version").get() as { user_version?: unknown } | undefined;
1218
return Number(row?.user_version ?? 0);
@@ -39,6 +45,6 @@ export function createNewerSqliteSchemaVersionError(
3945
`Run a build that supports schema ${schemaVersion} or newer against this state directory — rebuild or update the install above — or point this build at a different OPENCLAW_STATE_DIR. ` +
4046
`See ${OPENCLAW_DATABASE_SCHEMA_DOCS_URL}.`,
4147
);
42-
error.name = "SqliteSchemaVersionError";
48+
error.name = SQLITE_SCHEMA_VERSION_ERROR_NAME;
4349
return error;
4450
}

‎src/plugins/installed-plugin-index-record-state.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
inspectPluginInstallRecordMap,
44
type PluginInstallRecordMapState,
55
} from "../config/plugin-install-record-map.js";
6+
import { isSqliteSchemaVersionError } from "../infra/sqlite-user-version.js";
67
import { withExistingOpenClawStateDatabaseReadOnly } from "../state/openclaw-state-db-readonly.js";
78
import {
89
resolveInstalledPluginIndexStateDatabaseOptions,
@@ -48,7 +49,10 @@ export function inspectPersistedInstalledPluginIndexInstallRecordsSync(
4849
return parsed === undefined ? { status: "invalid" } : inspectPluginInstallRecordMap(parsed);
4950
}, resolveInstalledPluginIndexStateDatabaseOptions(options)) ?? { status: "missing" }
5051
);
51-
} catch {
52+
} catch (error) {
53+
if (isSqliteSchemaVersionError(error)) {
54+
throw error;
55+
}
5256
return { status: "invalid" };
5357
}
5458
}

‎src/plugins/installed-plugin-index-records.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import {
1010
setPluginInstallRecordMapEntry,
1111
} from "../config/plugin-install-record-map.js";
1212
import type { PluginInstallRecord } from "../config/types.plugins.js";
13+
import { requireNodeSqlite } from "../infra/node-sqlite.js";
1314
import {
1415
closeOpenClawStateDatabaseForTest,
16+
OPENCLAW_STATE_SCHEMA_VERSION,
1517
runOpenClawStateWriteTransaction,
1618
} from "../state/openclaw-state-db.js";
1719
import { withMockedWindowsPlatform } from "../test-utils/vitest-spies.js";
@@ -206,6 +208,34 @@ describe("plugin index install records store", () => {
206208
});
207209
});
208210

211+
it("preserves newer shared-state schema errors while loading install records", async () => {
212+
const stateDir = tempDirs.make("openclaw-plugin-index-records-");
213+
await writePersistedInstalledPluginIndexInstallRecords(
214+
{
215+
persisted: {
216+
source: "npm",
217+
218+
},
219+
},
220+
{ stateDir, candidates: [] },
221+
);
222+
closeOpenClawStateDatabaseForTest();
223+
const databasePath = resolveInstalledPluginIndexRecordsStorePath({ stateDir });
224+
const { DatabaseSync } = requireNodeSqlite();
225+
const database = new DatabaseSync(databasePath);
226+
database.exec(`PRAGMA user_version = ${OPENCLAW_STATE_SCHEMA_VERSION + 1};`);
227+
database.close();
228+
229+
expect(() => loadInstalledPluginIndexInstallRecordsSync({ stateDir })).toThrow(
230+
expect.objectContaining({
231+
name: "SqliteSchemaVersionError",
232+
message: expect.stringContaining(
233+
`uses newer schema version ${OPENCLAW_STATE_SCHEMA_VERSION + 1}`,
234+
),
235+
}),
236+
);
237+
});
238+
209239
it("returns prototype-safe map copies without cloning cached records", async () => {
210240
const stateDir = tempDirs.make("openclaw-plugin-index-records-");
211241
const candidate = createPluginCandidate(stateDir, "cached");

‎src/plugins/installed-plugin-index-store.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from "../infra/startup-migration-checkpoint.js";
1010
import {
1111
closeOpenClawStateDatabaseForTest,
12+
OPENCLAW_STATE_SCHEMA_VERSION,
1213
runOpenClawStateWriteTransaction,
1314
} from "../state/openclaw-state-db.js";
1415
import type { PluginCandidate } from "./discovery.js";
@@ -717,6 +718,24 @@ describe("installed plugin index persistence", () => {
717718
await expect(readPersistedInstalledPluginIndex({ stateDir })).resolves.toBeNull();
718719
});
719720

721+
it("preserves newer shared-state schema errors while reading the index", async () => {
722+
const stateDir = makeTempDir();
723+
await writePersistedInstalledPluginIndex(createIndex(), { stateDir });
724+
closeOpenClawStateDatabaseForTest();
725+
const databasePath = resolveInstalledPluginIndexStorePath({ stateDir });
726+
const { DatabaseSync } = requireNodeSqlite();
727+
const database = new DatabaseSync(databasePath);
728+
database.exec(`PRAGMA user_version = ${OPENCLAW_STATE_SCHEMA_VERSION + 1};`);
729+
database.close();
730+
731+
await expect(readPersistedInstalledPluginIndex({ stateDir })).rejects.toMatchObject({
732+
name: "SqliteSchemaVersionError",
733+
message: expect.stringContaining(
734+
`uses newer schema version ${OPENCLAW_STATE_SCHEMA_VERSION + 1}`,
735+
),
736+
});
737+
});
738+
720739
it("leaves retired JSON index files to the doctor migration owner", async () => {
721740
const stateDir = makeTempDir();
722741
const filePath = path.join(stateDir, "installs.json");

‎src/plugins/installed-plugin-index-store.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
setPluginInstallRecordMapEntry,
1414
} from "../config/plugin-install-record-map.js";
1515
import type { PluginInstallRecord } from "../config/types.plugins.js";
16+
import { isSqliteSchemaVersionError } from "../infra/sqlite-user-version.js";
1617
import { withExistingOpenClawStateDatabaseReadOnly } from "../state/openclaw-state-db-readonly.js";
1718
import { runOpenClawStateWriteTransaction } from "../state/openclaw-state-db.js";
1819
import { safeParseWithSchema } from "../utils/zod-parse.js";
@@ -355,7 +356,10 @@ function readPersistedInstalledPluginIndexFromSqlite(
355356
resolveInstalledPluginIndexStateDatabaseOptions(options),
356357
) ?? null
357358
);
358-
} catch {
359+
} catch (error) {
360+
if (isSqliteSchemaVersionError(error)) {
361+
throw error;
362+
}
359363
return null;
360364
}
361365
}

‎src/state/openclaw-agent-db.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
runSqliteImmediateTransactionSync,
1717
type SqliteTransactionOptions,
1818
} from "../infra/sqlite-transaction.js";
19+
import { isSqliteSchemaVersionError } from "../infra/sqlite-user-version.js";
1920
import {
2021
configureSqliteConnectionPragmas,
2122
configureSqlitePreSchemaPragmas,
@@ -351,7 +352,7 @@ export function openOpenClawAgentDatabase(
351352
db.close();
352353
if (
353354
err instanceof Error &&
354-
(err.name === "SqliteSchemaVersionError" || isTerminalSqliteIntegrityError(err))
355+
(isSqliteSchemaVersionError(err) || isTerminalSqliteIntegrityError(err))
355356
) {
356357
recordOpenClawAgentDatabaseOpenFailure(pathname, err);
357358
}

‎src/state/openclaw-state-db.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
runSqliteImmediateTransactionSync,
3030
type SqliteTransactionOptions,
3131
} from "../infra/sqlite-transaction.js";
32-
import { readSqliteUserVersion } from "../infra/sqlite-user-version.js";
32+
import { isSqliteSchemaVersionError, readSqliteUserVersion } from "../infra/sqlite-user-version.js";
3333
import {
3434
configureSqliteConnectionPragmas,
3535
configureSqlitePreSchemaPragmas,
@@ -594,7 +594,7 @@ function openUnpublishedOpenClawStateDatabase(
594594
db.close();
595595
if (
596596
err instanceof Error &&
597-
(err.name === "SqliteSchemaVersionError" || isTerminalSqliteIntegrityError(err))
597+
(isSqliteSchemaVersionError(err) || isTerminalSqliteIntegrityError(err))
598598
) {
599599
recordOpenClawStateDatabaseOpenFailure(pathname, err);
600600
}

0 commit comments

Comments
 (0)