Skip to content

Commit 40fb1ca

Browse files
authored
fix: schema-version refusal blames a downgrade that never happened and cannot identify the stale install (#115232)
* fix(state): name the refusing install in schema and exec-approval refusals The newer-schema refusal told operators not to downgrade and to upgrade OpenClaw, neither of which is actionable when two builds share one release version string. It now names the install root that refused, both schema versions, and warns that a linked source checkout reports its git HEAD even when its built dist is older. The exec-approvals gate told operators to run `openclaw doctor --fix` without naming the state directory, so a bare invocation repaired the default root while the scoped install stayed blocked. Both the TypeScript gate and its Swift sibling now scope the command to the blocked state directory. Refs #115008 * fix(gateway): name the refusing install in the startup schema refusal * fix(mac): keep the exec-approvals gate message buildable on iOS * fix(exec-approvals): shell-quote the state directory in the repair command * fix(exec-approvals): state the repair directory in prose so every shell can follow it
1 parent 4d9588d commit 40fb1ca

8 files changed

Lines changed: 111 additions & 7 deletions

File tree

‎apps/shared/OpenClawKit/Sources/OpenClawKit/ExecApprovalsLegacyMigrationGate.swift‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ enum ExecApprovalsLegacyMigrationGate {
2929
userInfo: [
3030
NSLocalizedDescriptionKey:
3131
"Legacy exec approvals exist at \(sourceURL.path). " +
32-
"Run `openclaw doctor --fix` before using exec approvals.",
32+
// Doctor repairs whichever state directory its own environment resolves to,
33+
// and an app store never shares the CLI's default root, so always name the
34+
// directory; a bare `doctor --fix` would repair a different one. Prose,
35+
// not a `VAR=value cmd` one-liner, so the path needs no shell quoting.
36+
"Run `openclaw doctor --fix` with OPENCLAW_STATE_DIR set to " +
37+
"\(stateDirectoryURL.path) before using exec approvals.",
3338
])
3439
}
3540
}

‎apps/shared/OpenClawKit/Tests/OpenClawKitTests/ExecApprovalsSQLiteStoreTests.swift‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,12 @@ struct ExecApprovalsSQLiteStoreTests {
213213
_ = try ExecApprovalsSQLiteStore.read(stateDirectoryURL: stateDirectoryURL)
214214
Issue.record("Expected pending legacy approvals to refuse SQLite access")
215215
} catch {
216-
#expect(error.localizedDescription.contains("Run `openclaw doctor --fix`"))
216+
// The blocked state directory must be named; a bare command repairs the
217+
// default root, and an app store never shares the CLI's default root.
218+
#expect(
219+
error.localizedDescription.contains(
220+
"Run `openclaw doctor --fix` with OPENCLAW_STATE_DIR set to "
221+
+ stateDirectoryURL.path))
217222
}
218223
}
219224
}

‎docs/reference/database-schemas.md‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,13 @@ Since 2026.7.2, `openclaw update` refuses to install a release that cannot open
8282

8383
### The Gateway refuses to start with a newer schema version error
8484

85-
A newer OpenClaw build wrote your databases, and the running build is older. The error and the Gateway startup log name the build that owns the database (`app_version`). Install that version or newer, or use one of the options above. Do not edit the database to silence the error.
85+
A newer OpenClaw build wrote your databases, and the running build is older. The error names the refusing install — release version, commit, and install root — plus the schema it supports and the schema it found.
86+
87+
Act on the install root, not the version. One release version string spans many `main` commits and several schema levels, so two installs can both call themselves `2026.7.2` and support different schemas. A prerelease version may not exist on the `latest` npm tag at all: check `npm view openclaw dist-tags` before reinstalling, because the tag carrying the schema you need may be `beta`, and reinstalling from `latest` can move you further away.
88+
89+
A linked source checkout is the case where the commit misleads: `openclaw --version` reports the checkout's git HEAD, but the code actually executing is whatever `dist/` was last built. If the install root is a checkout, rebuild it (`pnpm build`) before concluding the version is wrong.
90+
91+
Open the database with a build that supports its schema, or point the older build at a separate `OPENCLAW_STATE_DIR`. Do not edit the database to silence the error.
8692

8793
### A database is quarantined after integrity verification failed
8894

‎src/infra/exec-approvals-migration-gate.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
// Blocks runtime use while retired exec approval state still awaits Doctor import.
22
import fs from "node:fs";
3+
import path from "node:path";
34
import { resolveExecApprovalsPath } from "./exec-approvals-config.js";
45

56
const DOCTOR_CLAIM_SUFFIX = ".doctor-importing";
67
const legacyPresenceCache = new Map<string, boolean>();
78

9+
/**
10+
* Doctor repairs whichever state directory its own environment resolves to, so a bare
11+
* `openclaw doctor --fix` repairs the default root while a scoped install stays blocked.
12+
* Name the directory whenever this process is scoped to a non-default one. Say it in
13+
* prose rather than as a `VAR=value cmd` one-liner, which no Windows shell accepts.
14+
*/
15+
function doctorFixInstruction(filePath: string): string {
16+
const command = "Run `openclaw doctor --fix`";
17+
return process.env.OPENCLAW_STATE_DIR?.trim()
18+
? `${command} with OPENCLAW_STATE_DIR set to ${path.dirname(filePath)}`
19+
: command;
20+
}
21+
822
export class ExecApprovalsMigrationRequiredError extends Error {
923
constructor(filePath: string) {
1024
super(
11-
`Legacy exec approvals exist at ${filePath}. Run \`openclaw doctor --fix\` before using exec approvals.`,
25+
`Legacy exec approvals exist at ${filePath}. ${doctorFixInstruction(filePath)} before using exec approvals.`,
1226
);
1327
this.name = "ExecApprovalsMigrationRequiredError";
1428
}

‎src/infra/exec-approvals-store.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,24 @@ describe("exec approvals SQLite store", () => {
396396
expect(() => loadExecApprovals()).toThrow(ExecApprovalsMigrationRequiredError);
397397
});
398398

399+
it("scopes the doctor command to the blocked state directory", () => {
400+
// A bare `openclaw doctor --fix` repairs the default root, leaving a scoped
401+
// install blocked by the same file it was told to repair (#115008).
402+
const stateDir = process.env.OPENCLAW_STATE_DIR;
403+
if (!stateDir) {
404+
throw new Error("missing test state dir");
405+
}
406+
const error = new ExecApprovalsMigrationRequiredError(
407+
path.join(stateDir, "exec-approvals.json"),
408+
);
409+
410+
// Prose, not `VAR=value cmd`: no Windows shell accepts that form, and a path
411+
// containing spaces would need shell-specific quoting to survive a paste.
412+
expect(error.message).toContain(
413+
`Run \`openclaw doctor --fix\` with OPENCLAW_STATE_DIR set to ${stateDir}`,
414+
);
415+
});
416+
399417
it.each([
400418
[true, false, false],
401419
[false, true, false],

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Tests for SQLite user_version pragma helper.
22
import { describe, expect, it } from "vitest";
3+
import { VERSION } from "../version.js";
34
import {
45
createNewerSqliteSchemaVersionError,
6+
describeRunningOpenClawBuild,
57
readSqliteUserVersion,
68
} from "./sqlite-user-version.js";
79

@@ -56,4 +58,31 @@ describe("createNewerSqliteSchemaVersionError", () => {
5658
expect(error.name).toBe("SqliteSchemaVersionError");
5759
expect(error.message).toContain("https://docs.openclaw.ai/reference/database-schemas");
5860
});
61+
62+
it("names the refusing install and both schema versions", () => {
63+
const error = createNewerSqliteSchemaVersionError("test database", "/tmp/test.sqlite", 12, 11);
64+
65+
expect(error.message).toContain("uses newer schema version 12");
66+
expect(error.message).toContain("this build supports 11");
67+
expect(error.message).toContain(describeRunningOpenClawBuild());
68+
expect(error.message).toContain("supports schema 12 or newer");
69+
});
70+
71+
it("does not assert a downgrade the operator never performed", () => {
72+
// Two builds sharing one release version can support different schemas (#115008).
73+
// Telling the operator to upgrade or stop downgrading is unactionable and often wrong.
74+
const error = createNewerSqliteSchemaVersionError("test database", "/tmp/test.sqlite", 12, 11);
75+
76+
expect(error.message).not.toContain("Do not downgrade");
77+
expect(error.message).not.toContain("Upgrade OpenClaw");
78+
});
79+
});
80+
81+
describe("describeRunningOpenClawBuild", () => {
82+
it("reports the version and the install root operators can act on", () => {
83+
const described = describeRunningOpenClawBuild();
84+
85+
expect(described).toContain(VERSION);
86+
expect(described).toContain("installed at ");
87+
});
5988
});

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import { OPENCLAW_DATABASE_SCHEMA_DOCS_URL } from "../state/openclaw-state-db-contract.js";
2+
import { VERSION } from "../version.js";
3+
import { resolveCommitHash } from "./git-commit.js";
4+
import { resolveOpenClawPackageRootSync } from "./openclaw-root.js";
5+
16
type SqliteUserVersionReader = {
27
prepare: (sql: string) => { get: () => unknown };
38
};
@@ -7,14 +12,32 @@ export function readSqliteUserVersion(db: SqliteUserVersionReader): number {
712
return Number(row?.user_version ?? 0);
813
}
914

15+
/**
16+
* Name the refusing install the way `--version` does, plus the root it runs from.
17+
* The path is the only part an operator can always act on: one release version
18+
* string spans many commits, and a linked source checkout reports its git HEAD
19+
* even when the built output actually executing is older.
20+
*/
21+
export function describeRunningOpenClawBuild(): string {
22+
const moduleUrl = import.meta.url;
23+
const commit = resolveCommitHash({ moduleUrl });
24+
const root = resolveOpenClawPackageRootSync({ moduleUrl });
25+
const identity = commit ? `OpenClaw ${VERSION} (${commit})` : `OpenClaw ${VERSION}`;
26+
return root ? `${identity} installed at ${root}` : identity;
27+
}
28+
1029
export function createNewerSqliteSchemaVersionError(
1130
databaseLabel: string,
1231
pathname: string,
1332
schemaVersion: number,
1433
supportedVersion: number,
1534
): Error {
1635
const error = new Error(
17-
`${databaseLabel} ${pathname} uses newer schema version ${schemaVersion}; this OpenClaw build supports ${supportedVersion}. Upgrade OpenClaw before opening this database. Do not downgrade OpenClaw or modify the database. To run this older build, use a separate state directory or restore a compatible backup. See https://docs.openclaw.ai/reference/database-schemas.`,
36+
`${databaseLabel} ${pathname} uses newer schema version ${schemaVersion}; this build supports ${supportedVersion}. ` +
37+
`Refused by ${describeRunningOpenClawBuild()}. ` +
38+
"Identify installs by that path: one version string spans many builds, and a linked source checkout reports its git HEAD even when its built output is older. " +
39+
`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. ` +
40+
`See ${OPENCLAW_DATABASE_SCHEMA_DOCS_URL}.`,
1841
);
1942
error.name = "SqliteSchemaVersionError";
2043
return error;

‎src/state/openclaw-database-preflight.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import {
77
getNodeSqliteKysely,
88
} from "../infra/kysely-sync.js";
99
import { openNodeSqliteDatabase } from "../infra/node-sqlite.js";
10-
import { readSqliteUserVersion } from "../infra/sqlite-user-version.js";
10+
import {
11+
describeRunningOpenClawBuild,
12+
readSqliteUserVersion,
13+
} from "../infra/sqlite-user-version.js";
1114
import type { OpenClawSchemaVersions } from "./openclaw-schema-versions.js";
1215
import type { DB as OpenClawStateKyselyDatabase } from "./openclaw-state-db.generated.js";
1316
import {
@@ -44,7 +47,8 @@ type AgentRegistryDatabase = Pick<OpenClawStateKyselyDatabase, "agent_databases"
4447
export class OpenClawDatabaseSchemaPreflightError extends Error {
4548
constructor(readonly incompatibleDatabases: readonly IncompatibleOpenClawDatabase[]) {
4649
super(
47-
`Gateway refused startup because ${incompatibleDatabases.length} OpenClaw database schema(s) are newer than this build. See ${OPENCLAW_DATABASE_SCHEMA_DOCS_URL}.`,
50+
`Gateway refused startup because ${incompatibleDatabases.length} OpenClaw database schema(s) are newer than this build. ` +
51+
`Refused by ${describeRunningOpenClawBuild()}. See ${OPENCLAW_DATABASE_SCHEMA_DOCS_URL}.`,
4852
);
4953
this.name = "OpenClawDatabaseSchemaPreflightError";
5054
}

0 commit comments

Comments
 (0)