@@ -12,7 +12,10 @@ import { resolveGatewayLockDir } from "../config/paths.js";
1212import type { OpenClawConfig } from "../config/types.openclaw.js" ;
1313import { hasActiveStartupMigrationLease } from "../infra/startup-migration-checkpoint.js" ;
1414import { getFileLockProcessStartTime } from "../shared/pid-alive.js" ;
15- import { ensureOpenClawAgentDatabaseSchema } from "../state/openclaw-agent-db.js" ;
15+ import {
16+ ensureOpenClawAgentDatabaseSchema ,
17+ OPENCLAW_AGENT_SCHEMA_VERSION ,
18+ } from "../state/openclaw-agent-db.js" ;
1619
1720const STARTUP_REFUSAL =
1821 "OpenClaw startup migrations did not complete cleanly; refusing to report the gateway ready." ;
@@ -160,7 +163,100 @@ function seedOwnerlessSchemaOnlyAgentDatabase(stateDir: string): string {
160163 return databasePath ;
161164}
162165
166+ function seedV17AdditiveRepairDatabase ( stateDir : string ) : string {
167+ const databasePath = path . join ( stateDir , "agents" , "main" , "agent" , "openclaw-agent.sqlite" ) ;
168+ fs . mkdirSync ( path . dirname ( databasePath ) , { recursive : true } ) ;
169+ const database = new DatabaseSync ( databasePath ) ;
170+ try {
171+ ensureOpenClawAgentDatabaseSchema ( database , {
172+ agentId : "main" ,
173+ env : { ...process . env , OPENCLAW_STATE_DIR : stateDir } ,
174+ path : databasePath ,
175+ register : false ,
176+ } ) ;
177+ database . exec ( `
178+ DROP TABLE session_participants;
179+ DROP TRIGGER session_conversations_route_context_invalidate_after_update;
180+ ALTER TABLE session_conversations DROP COLUMN route_context_json;
181+ DROP INDEX idx_agent_transcript_event_identity_sequence;
182+ PRAGMA user_version = 17;
183+ UPDATE schema_meta SET schema_version = 17;
184+ ` ) ;
185+ } finally {
186+ database . close ( ) ;
187+ }
188+ return databasePath ;
189+ }
190+
163191describe ( "doctor invalid config process exit" , ( ) => {
192+ it ( "repairs the v17 additive schema through doctor --fix" , ( ) => {
193+ const root = fs . realpathSync ( tempDirs . make ( "openclaw-doctor-v17-additive-" ) ) ;
194+ const stateDir = path . join ( root , "state" ) ;
195+ const configPath = path . join ( stateDir , "openclaw.json" ) ;
196+ fs . mkdirSync ( path . join ( stateDir , "agents" , "main" , "sessions" ) , { recursive : true } ) ;
197+ fs . writeFileSync ( configPath , "{}\n" ) ;
198+ const databasePath = seedV17AdditiveRepairDatabase ( stateDir ) ;
199+ const runtimeRoot = createSourceRuntime ( root ) ;
200+ const env : NodeJS . ProcessEnv = {
201+ ...process . env ,
202+ OPENCLAW_CONFIG_PATH : configPath ,
203+ OPENCLAW_DISABLE_BUNDLED_PLUGINS : "1" ,
204+ OPENCLAW_STATE_DIR : stateDir ,
205+ OPENCLAW_TEST_FAST : "1" ,
206+ NO_COLOR : "1" ,
207+ } ;
208+ const args = [
209+ path . join ( runtimeRoot , "src" , "entry.ts" ) ,
210+ "doctor" ,
211+ "--fix" ,
212+ "--non-interactive" ,
213+ "--yes" ,
214+ "--no-workspace-suggestions" ,
215+ ] ;
216+
217+ const first = runSourceRuntime ( runtimeRoot , env , args , 60_000 ) ;
218+ expect ( first . error , first . stderr ) . toBeUndefined ( ) ;
219+ expect ( first . status , first . stderr ) . toBe ( 0 ) ;
220+ expect ( `${ first . stdout } \n${ first . stderr } ` ) . toContain ( "v17 -> v19" ) ;
221+
222+ const repaired = new DatabaseSync ( databasePath , { readOnly : true } ) ;
223+ try {
224+ expect ( repaired . prepare ( "PRAGMA user_version" ) . get ( ) ?. user_version ) . toBe (
225+ OPENCLAW_AGENT_SCHEMA_VERSION ,
226+ ) ;
227+ expect (
228+ repaired
229+ . prepare (
230+ "SELECT name FROM pragma_table_info('session_conversations') WHERE name = 'route_context_json'" ,
231+ )
232+ . get ( ) ?. name ,
233+ ) . toBe ( "route_context_json" ) ;
234+ expect (
235+ repaired
236+ . prepare (
237+ "SELECT name FROM sqlite_schema WHERE type = 'trigger' AND name = 'session_conversations_route_context_invalidate_after_update'" ,
238+ )
239+ . get ( ) ?. name ,
240+ ) . toBe ( "session_conversations_route_context_invalidate_after_update" ) ;
241+ expect (
242+ repaired
243+ . prepare (
244+ "SELECT name FROM sqlite_schema WHERE type = 'index' AND name = 'idx_agent_transcript_event_identity_sequence'" ,
245+ )
246+ . get ( ) ?. name ,
247+ ) . toBe ( "idx_agent_transcript_event_identity_sequence" ) ;
248+ } finally {
249+ repaired . close ( ) ;
250+ }
251+
252+ const second = runSourceRuntime ( runtimeRoot , env , args , 60_000 ) ;
253+ expect ( second . error , second . stderr ) . toBeUndefined ( ) ;
254+ expect ( second . status , second . stderr ) . toBe ( 0 ) ;
255+ expect ( `${ second . stdout } \n${ second . stderr } ` ) . not . toMatch (
256+ / S k i p p e d a g e n t d a t a b a s e m i g r a t i o n | U p g r a d e d a g e n t d a t a b a s e s c h e m a / u,
257+ ) ;
258+ } ) ;
259+
164260 it ( "keeps Doctor UI checks inside the source runtime fixture" , ( ) => {
165261 const root = fs . realpathSync ( tempDirs . make ( "openclaw-doctor-runtime-owner-" ) ) ;
166262 const runtimeRoot = createSourceRuntime ( root ) ;
0 commit comments