@@ -8,22 +8,20 @@ import {
88 executeSqliteQueryTakeFirstSync ,
99 getNodeSqliteKysely ,
1010} from "../../infra/kysely-sync.js" ;
11- import {
12- normalizeAgentId ,
13- normalizeMainKey ,
14- parseAgentSessionKey ,
15- } from "../../routing/session-key.js" ;
16- import { withOpenClawAgentDatabaseReadOnly } from "../../state/openclaw-agent-db-readonly.js" ;
11+ import { normalizeAgentId , normalizeMainKey } from "../../routing/session-key.js" ;
1712import { isSameOpenClawAgentDatabasePath } from "../../state/openclaw-agent-db-registry.js" ;
1813import { withExistingOpenClawStateDatabaseReadOnly } from "../../state/openclaw-state-db-readonly.js" ;
1914import type { DB as OpenClawStateKyselyDatabase } from "../../state/openclaw-state-db.generated.js" ;
2015import { runOpenClawStateWriteTransaction } from "../../state/openclaw-state-db.js" ;
2116import { resolveStateDir } from "../paths.js" ;
2217import type { OpenClawConfig } from "../types.openclaw.js" ;
18+ import {
19+ readClaimsFromStore ,
20+ storeHasLegacyAgentSessionKey ,
21+ } from "./legacy-main-session-key-scan.js" ;
2322import {
2423 claimsMatch ,
2524 processIdenticalClaims ,
26- readClaim ,
2725 repairDivergentClaims ,
2826 samePhysicalStore ,
2927 warningForDivergence ,
@@ -36,11 +34,11 @@ import type {
3634 SessionClaim ,
3735} from "./legacy-main-session-migration.contract.js" ;
3836import { resolveSessionStorePathCore } from "./paths.js" ;
39- import { getSessionKysely } from "./session-accessor.sqlite-scope.js" ;
4037import { resolveSqliteTargetFromSessionStorePath } from "./session-sqlite-target.js" ;
4138import {
4239 resolveAllAgentSessionStoreCandidateTargetsSync ,
4340 resolveAgentSessionStoreTargetsSync ,
41+ resolveSessionStoreCompatibilityAgentId ,
4442} from "./targets.js" ;
4543
4644const SOURCE_KEY = "legacy-main-session-keys" ;
@@ -82,15 +80,6 @@ function resolveArmingDecision(cfg: OpenClawConfig, legacyAgentId: string): Armi
8280 return { armed : false , reason : "owner-unresolved" } ;
8381}
8482
85- function canonicalKeyFor ( key : string , legacyAgentId : string , ownerAgentId : string ) : string | null {
86- const parsed = parseAgentSessionKey ( key ) ;
87- if ( ! parsed || normalizeAgentId ( parsed . agentId ) !== legacyAgentId ) {
88- return null ;
89- }
90- const prefix = `agent:${ parsed . agentId } :` ;
91- return key . startsWith ( prefix ) ? `agent:${ ownerAgentId } :${ key . slice ( prefix . length ) } ` : null ;
92- }
93-
9483function addPhysicalStore ( stores : PhysicalStore [ ] , candidate : PhysicalStore ) : void {
9584 if ( ! stores . some ( ( store ) => samePhysicalStore ( store , candidate ) ) ) {
9685 stores . push ( candidate ) ;
@@ -160,7 +149,7 @@ function resolvePhysicalStores(params: {
160149 env : NodeJS . ProcessEnv ;
161150 legacyAgentId : string ;
162151 mode : LegacyMainSessionMigrationMode ;
163- ownerAgentId : string ;
152+ ownerAgentId ? : string ;
164153} ) : ResolvedPhysicalStores {
165154 const logicalTargets = [
166155 ...resolveAllAgentSessionStoreCandidateTargetsSync ( params . cfg , { env : params . env } ) ,
@@ -172,14 +161,17 @@ function resolvePhysicalStores(params: {
172161 env : params . env ,
173162 } ) ,
174163 } ,
175- {
164+ ] ;
165+ if ( params . ownerAgentId ) {
166+ logicalTargets . push ( {
176167 agentId : params . ownerAgentId ,
177168 storePath : resolveSessionStorePathCore ( params . cfg . session ?. store , {
178169 agentId : params . ownerAgentId ,
179170 env : params . env ,
180171 } ) ,
181- } ,
182- ] ;
172+ } ) ;
173+ }
174+ const defaultAgentId = params . ownerAgentId ?? resolveSessionStoreCompatibilityAgentId ( params . cfg ) ;
183175 const stores : PhysicalStore [ ] = [ ] ;
184176 const jsonPaths = new Set < string > ( ) ;
185177 const unreadable : LegacyMainSessionMigrationOutcome [ ] = [ ] ;
@@ -190,7 +182,7 @@ function resolvePhysicalStores(params: {
190182 }
191183 const resolved = resolveSqliteTargetFromSessionStorePath ( target . storePath , {
192184 agentId : target . agentId ,
193- defaultAgentId : params . ownerAgentId ,
185+ defaultAgentId,
194186 env : params . env ,
195187 } ) ;
196188 const physical : PhysicalStore = {
@@ -229,47 +221,6 @@ function resolveSourceLayout(resolved: ResolvedPhysicalStores): string[] {
229221 ] . toSorted ( ) ;
230222}
231223
232- function readClaimsFromStore ( params : {
233- legacyAgentId : string ;
234- ownerAgentId : string ;
235- store : PhysicalStore ;
236- env : NodeJS . ProcessEnv ;
237- } ) : { canonical : SessionClaim [ ] ; legacy : SessionClaim [ ] } {
238- if ( inspectPath ( params . store . path ) === "missing" ) {
239- return { canonical : [ ] , legacy : [ ] } ;
240- }
241- const result = withOpenClawAgentDatabaseReadOnly (
242- ( database ) => {
243- const keys = executeSqliteQuerySync (
244- database . db ,
245- getSessionKysely ( database . db ) . selectFrom ( "session_nodes" ) . select ( "session_key" ) ,
246- ) . rows . map ( ( row ) => row . session_key ) ;
247- const legacy : SessionClaim [ ] = [ ] ;
248- const canonical : SessionClaim [ ] = [ ] ;
249- for ( const key of keys ) {
250- const canonicalKey = canonicalKeyFor ( key , params . legacyAgentId , params . ownerAgentId ) ;
251- if ( canonicalKey ) {
252- const claim = readClaim ( database , params . store , key , canonicalKey ) ;
253- if ( claim ) {
254- legacy . push ( claim ) ;
255- }
256- continue ;
257- }
258- const parsed = parseAgentSessionKey ( key ) ;
259- if ( parsed && normalizeAgentId ( parsed . agentId ) === params . ownerAgentId ) {
260- const claim = readClaim ( database , params . store , key , key ) ;
261- if ( claim ) {
262- canonical . push ( claim ) ;
263- }
264- }
265- }
266- return { canonical, legacy } ;
267- } ,
268- { agentId : params . store . databaseAgentId , env : params . env , path : params . store . path } ,
269- ) ;
270- return result . found ? result . value : { canonical : [ ] , legacy : [ ] } ;
271- }
272-
273224function readLedger ( env : NodeJS . ProcessEnv ) : { report : LedgerReport ; status : string } | undefined {
274225 return (
275226 withExistingOpenClawStateDatabaseReadOnly (
@@ -420,6 +371,38 @@ async function migrateLegacyMainSessionKeysInternal(params: {
420371 warnings : [ ] as string [ ] ,
421372 } ;
422373 if ( ! arming . armed ) {
374+ if ( arming . reason === "owner-unresolved" ) {
375+ // Owner guidance is useful only when legacy rows may exist; unreadable or JSON
376+ // candidates fail open because they cannot prove the fleet is clean.
377+ let rowsMayExist : boolean ;
378+ try {
379+ const resolved = resolvePhysicalStores ( {
380+ cfg : params . cfg ,
381+ env,
382+ legacyAgentId,
383+ mode : params . mode ,
384+ } ) ;
385+ rowsMayExist =
386+ resolved . unreadable . length > 0 ||
387+ resolved . jsonPaths . length > 0 ||
388+ resolved . stores . some (
389+ ( store ) =>
390+ inspectPath ( store . path ) === "present" &&
391+ storeHasLegacyAgentSessionKey ( { env, legacyAgentId, store } ) ,
392+ ) ;
393+ } catch {
394+ rowsMayExist = true ;
395+ }
396+ if ( ! rowsMayExist ) {
397+ return {
398+ ...base ,
399+ armed : false ,
400+ complete : true ,
401+ ledgerComplete : false ,
402+ outcomes : [ { kind : "no-legacy-rows" , detail : "no configured owner" } ] ,
403+ } ;
404+ }
405+ }
423406 const unresolved = arming . reason === "owner-unresolved" ;
424407 return {
425408 ...base ,
@@ -497,6 +480,9 @@ async function migrateLegacyMainSessionKeysInternal(params: {
497480 const allCanonical : SessionClaim [ ] = [ ] ;
498481 for ( const store of resolved . stores ) {
499482 try {
483+ if ( inspectPath ( store . path ) === "missing" ) {
484+ continue ;
485+ }
500486 const claims = readClaimsFromStore ( { env, legacyAgentId, ownerAgentId, store } ) ;
501487 allLegacy . push ( ...claims . legacy ) ;
502488 allCanonical . push ( ...claims . canonical ) ;
0 commit comments