@@ -9,8 +9,10 @@ import {
99 parseSqliteSessionFileMarker ,
1010 sqliteSessionFileMarkerMatchesTarget ,
1111} from "../config/sessions/legacy-sqlite-marker.js" ;
12+ import { resolveSessionEntryAccessTarget } from "../config/sessions/session-accessor.js" ;
1213import { resolveSessionStorePathForScope } from "../config/sessions/session-store-path.js" ;
1314import type { SessionEntry } from "../config/sessions/types.js" ;
15+ import type { OpenClawConfig } from "../config/types.openclaw.js" ;
1416import {
1517 createPluginBlobStore ,
1618 type OpenBlobStoreOptions ,
@@ -23,7 +25,12 @@ import {
2325 type PluginStateKeyedStore ,
2426 type PluginStateSyncKeyedStore ,
2527} from "../plugin-state/plugin-state-store.js" ;
26- import { normalizeAgentId , parseAgentSessionKey } from "../routing/session-key.js" ;
28+ import {
29+ classifySessionKeyShape ,
30+ isUnscopedSessionKeySentinel ,
31+ normalizeAgentId ,
32+ parseAgentSessionKey ,
33+ } from "../routing/session-key.js" ;
2734import {
2835 isAgentHarnessSessionKey ,
2936 isAgentHarnessSessionKeyOwnedBy ,
@@ -79,6 +86,8 @@ const PLUGIN_GATEWAY_GLOBAL_SESSION_MUTATION_METHODS = new Set([
7986
8087export function createPluginRuntimeResolver ( state : PluginRegistryState ) {
8188 const { registry, registryParams } = state ;
89+ // SAFETY: Logical session resolution only reads the immutable runtime config snapshot.
90+ const currentSessionConfig = ( ) => registryParams . runtime . config . current ( ) as OpenClawConfig ;
8291 const pluginRuntimeById = new Map < string , PluginRuntime > ( ) ;
8392 const pluginRuntimeRecordById = new Map < string , PluginRecord > ( ) ;
8493 const activePluginRuntimeRecords = new WeakSet < PluginRecord > ( ) ;
@@ -319,50 +328,71 @@ export function createPluginRuntimeResolver(state: PluginRegistryState) {
319328 }
320329 assertReservedSessionKeyOwned ( params . sessionKey , params . action ) ;
321330 } ;
331+ const resolveStoredSessionOwnershipTarget = ( params : {
332+ agentId ?: string ;
333+ env ?: NodeJS . ProcessEnv ;
334+ sessionKey : string ;
335+ storePath ?: string ;
336+ } ) : { entry ?: SessionEntry ; sessionKey : string } => {
337+ if (
338+ classifySessionKeyShape ( params . sessionKey ) === "legacy_or_alias" &&
339+ ! isUnscopedSessionKeySentinel ( params . sessionKey ) &&
340+ params . agentId === undefined &&
341+ params . storePath === undefined
342+ ) {
343+ // Logical keys need their configured agent before SQLite ownership admission.
344+ const target = resolveSessionEntryAccessTarget ( {
345+ cfg : currentSessionConfig ( ) ,
346+ sessionKey : params . sessionKey ,
347+ ...( params . env !== undefined ? { env : params . env } : { } ) ,
348+ } ) ;
349+ return { entry : target . entry , sessionKey : target . canonicalKey } ;
350+ }
351+ return {
352+ entry : registryParams . runtime . agent . session . getSessionEntry ( {
353+ sessionKey : params . sessionKey ,
354+ readConsistency : "latest" ,
355+ ...( params . agentId !== undefined ? { agentId : params . agentId } : { } ) ,
356+ ...( params . env !== undefined ? { env : params . env } : { } ) ,
357+ ...( params . storePath !== undefined ? { storePath : params . storePath } : { } ) ,
358+ } ) ,
359+ sessionKey : params . sessionKey ,
360+ } ;
361+ } ;
322362 const assertStoredSessionEntryOwned = ( params : {
323363 action : string ;
324364 agentId ?: string ;
325365 env ?: NodeJS . ProcessEnv ;
326366 sessionKey : string ;
327367 storePath ?: string ;
328368 } ) : SessionEntry | undefined => {
329- const entry = registryParams . runtime . agent . session . getSessionEntry ( {
330- sessionKey : params . sessionKey ,
331- readConsistency : "latest" ,
332- ...( params . agentId !== undefined ? { agentId : params . agentId } : { } ) ,
333- ...( params . env !== undefined ? { env : params . env } : { } ) ,
334- ...( params . storePath !== undefined ? { storePath : params . storePath } : { } ) ,
335- } ) ;
336- assertSessionEntryOwned ( { action : params . action , entry, sessionKey : params . sessionKey } ) ;
337- return entry ;
369+ const target = resolveStoredSessionOwnershipTarget ( params ) ;
370+ assertSessionEntryOwned ( { action : params . action , ...target } ) ;
371+ return target . entry ;
338372 } ;
339373 const resolveStoredSessionExecutionOwner = ( params : {
340374 action : string ;
341375 agentId ?: string ;
342376 sessionKey : string ;
343377 storePath ?: string ;
344378 } ) : string | undefined => {
345- const entry = registryParams . runtime . agent . session . getSessionEntry ( {
346- sessionKey : params . sessionKey ,
347- readConsistency : "latest" ,
348- ...( params . agentId !== undefined ? { agentId : params . agentId } : { } ) ,
349- ...( params . storePath !== undefined ? { storePath : params . storePath } : { } ) ,
350- } ) ;
379+ const target = resolveStoredSessionOwnershipTarget ( params ) ;
380+ const { entry, sessionKey } = target ;
351381 const locked = entry
352- ? resolveLockedSessionHarnessRegistration ( params . sessionKey , entry , params . action )
382+ ? resolveLockedSessionHarnessRegistration ( sessionKey , entry , params . action )
353383 : undefined ;
354384 if ( ! entry || ! locked || locked . ownerPluginId === pluginId ) {
355- assertSessionEntryOwned ( { action : params . action , entry , sessionKey : params . sessionKey } ) ;
385+ assertSessionEntryOwned ( { action : params . action , ... target } ) ;
356386 return undefined ;
357387 }
358388 const registration = "registration" in locked ? locked . registration : undefined ;
359389 if ( ! registration ) {
360390 throw new Error (
361- `Locked session "${ params . sessionKey } " is owned by plugin "${ locked . ownerPluginId } ", not "${ pluginId } ".` ,
391+ `Locked session "${ sessionKey } " is owned by plugin "${ locked . ownerPluginId } ", not "${ pluginId } ".` ,
362392 ) ;
363393 }
364394 if ( ! registration . harness . delegatedExecutionPluginIds ?. includes ( pluginId ) ) {
365- assertLockedSessionEntryOwned ( params . sessionKey , entry , params . action ) ;
395+ assertLockedSessionEntryOwned ( sessionKey , entry , params . action ) ;
366396 }
367397 return locked . ownerPluginId ;
368398 } ;
0 commit comments