1+ import fs from "node:fs" ;
12import { resolveAgentSessionDirs } from "../../agents/session-dirs.js" ;
23import { migrateOrphanedSessionKeys } from "../../infra/state-migrations.session-store.js" ;
34import type { PreparedLegacySessionSurfaces } from "../../plugins/legacy-session-surfaces.types.js" ;
5+ import { listOpenClawRegisteredAgentDatabases } from "../../state/openclaw-agent-db-registry.js" ;
46import {
57 closeOpenClawAgentDatabaseByPath ,
68 isOpenClawAgentDatabaseOpen ,
@@ -9,7 +11,10 @@ import {
911import { resolveStateDir } from "../paths.js" ;
1012import type { OpenClawConfig } from "../types.openclaw.js" ;
1113import { migrateLegacyMainSessionKeys } from "./legacy-main-session-migration.js" ;
12- import { setCanonicalSqliteSessionMainKey } from "./session-canonical-key.js" ;
14+ import {
15+ isCanonicalSqliteSessionMainKeyCurrent ,
16+ setCanonicalSqliteSessionMainKey ,
17+ } from "./session-canonical-key.js" ;
1318import { resolveSqliteTargetFromSessionStorePath } from "./session-sqlite-target.js" ;
1419import { sweepOrphanSessionStoreTemps } from "./store-temp-cleanup.js" ;
1520import { resolveAllAgentSessionStoreTargetsSync } from "./targets.js" ;
@@ -22,6 +27,12 @@ type PrepareLegacySessionSurfaces = (params: {
2227 env ?: NodeJS . ProcessEnv ;
2328} ) => PreparedLegacySessionSurfaces ;
2429
30+ type SessionSqliteDatabaseExists = ( params : {
31+ agentId : string ;
32+ env ?: NodeJS . ProcessEnv ;
33+ path ?: string ;
34+ } ) => boolean ;
35+
2536/** Runs best-effort session migration and orphan-temp cleanup before runtime reads. */
2637export async function runSessionStartupMigration ( params : {
2738 cfg : OpenClawConfig ;
@@ -33,6 +44,7 @@ export async function runSessionStartupMigration(params: {
3344 prepareLegacySessionSurfaces ?: PrepareLegacySessionSurfaces ;
3445 migrateManagedWorktreeCanonicalWorkspaces ?: typeof migrateManagedWorktreeCanonicalWorkspaces ;
3546 resolveAllAgentSessionStoreTargetsSync ?: typeof resolveAllAgentSessionStoreTargetsSync ;
47+ sessionSqliteDatabaseExists ?: SessionSqliteDatabaseExists ;
3648 sweepOrphanSessionStoreTemps ?: typeof sweepOrphanSessionStoreTemps ;
3749 } ;
3850} ) : Promise < boolean > {
@@ -64,7 +76,11 @@ export async function runSessionStartupMigration(params: {
6476 const result = await migrate ( {
6577 cfg : params . cfg ,
6678 env,
67- legacySessionSurfaces : prepareSurfaces ( { config : params . cfg , env } ) ,
79+ legacySessionSurfaces : ( ) =>
80+ prepareSurfaces ( {
81+ config : params . cfg ,
82+ env,
83+ } ) ,
6884 } ) ;
6985 if ( result . changes . length > 0 ) {
7086 params . log . info (
@@ -109,17 +125,45 @@ export async function runSessionStartupMigration(params: {
109125 }
110126
111127 const sweepTemps = params . deps ?. sweepOrphanSessionStoreTemps ?? sweepOrphanSessionStoreTemps ;
128+ const databaseExists =
129+ params . deps ?. sessionSqliteDatabaseExists ??
130+ ( ( input : Parameters < SessionSqliteDatabaseExists > [ 0 ] ) =>
131+ input . path !== undefined && fs . existsSync ( input . path ) ) ;
112132 try {
113133 let removedFiles = 0 ;
114134 let migratedWorktreeSessions = 0 ;
135+ const registeredDatabases = new Set (
136+ listOpenClawRegisteredAgentDatabases ( { env } ) . map (
137+ ( entry ) => `${ entry . agentId } \0${ entry . path } ` ,
138+ ) ,
139+ ) ;
115140 for ( const target of targets ) {
116141 const path = resolveSqliteTargetFromSessionStorePath ( target . storePath , {
117142 agentId : target . agentId ,
118143 env : params . env ,
119144 } ) . path ;
145+ if (
146+ ! databaseExists ( {
147+ agentId : target . agentId ,
148+ ...( params . env ? { env : params . env } : { } ) ,
149+ path,
150+ } )
151+ ) {
152+ removedFiles += await sweepTemps ( { storePath : target . storePath } ) ;
153+ continue ;
154+ }
120155 const alreadyOpen = isOpenClawAgentDatabaseOpen ( path ) ;
121- const database = openOpenClawAgentDatabase ( { agentId : target . agentId , path } ) ;
122- setCanonicalSqliteSessionMainKey ( database , params . cfg . session ?. mainKey ) ;
156+ const isRegistered = registeredDatabases . has ( `${ target . agentId } \0${ path } ` ) ;
157+ if (
158+ ! isRegistered ||
159+ ! isCanonicalSqliteSessionMainKeyCurrent (
160+ { agentId : target . agentId , env, path } ,
161+ params . cfg . session ?. mainKey ,
162+ )
163+ ) {
164+ const database = openOpenClawAgentDatabase ( { agentId : target . agentId , env, path } ) ;
165+ setCanonicalSqliteSessionMainKey ( database , params . cfg . session ?. mainKey ) ;
166+ }
123167 const migrateWorktreeSessions =
124168 params . deps ?. migrateManagedWorktreeCanonicalWorkspaces ??
125169 migrateManagedWorktreeCanonicalWorkspaces ;
@@ -135,7 +179,7 @@ export async function runSessionStartupMigration(params: {
135179 `session: managed-worktree workspace migration failed for ${ target . agentId } ; continuing: ${ String ( error ) } ` ,
136180 ) ;
137181 }
138- if ( ! alreadyOpen ) {
182+ if ( ! alreadyOpen && isOpenClawAgentDatabaseOpen ( path ) ) {
139183 closeOpenClawAgentDatabaseByPath ( path ) ;
140184 }
141185 removedFiles += await sweepTemps ( { storePath : target . storePath } ) ;
0 commit comments