@@ -11,6 +11,7 @@ import type { OpenClawConfig } from "../../config/types.openclaw.js";
1111import { resolveGatewaySessionStoreTarget } from "../../gateway/session-utils.js" ;
1212import { getAgentEventLifecycleGeneration } from "../../infra/agent-events.js" ;
1313import { listAgentRunsForSession } from "../../infra/agent-run-registry.js" ;
14+ import { collectActiveSessionWorkAdmissions } from "../../sessions/session-lifecycle-admission.js" ;
1415import {
1516 listActiveEmbeddedRunSessionIds ,
1617 listActiveEmbeddedRunSessionKeys ,
@@ -37,7 +38,13 @@ async function markRecoveryStore(params: {
3738 entry : SessionEntry ,
3839 sessionKey : string ,
3940 ) =>
40- | { action : "mark" ; replaceRuns ?: boolean ; resetRuntime ?: boolean ; runs ?: RestartRecoveryRun [ ] }
41+ | {
42+ action : "mark" ;
43+ forceRestartSafeTools ?: boolean ;
44+ replaceRuns ?: boolean ;
45+ resetRuntime ?: boolean ;
46+ runs ?: RestartRecoveryRun [ ] ;
47+ }
4148 | { action : "retire_terminal" }
4249 | undefined ;
4350} ) {
@@ -71,6 +78,9 @@ async function markRecoveryStore(params: {
7178 if ( plan . replaceRuns ) {
7279 entry . restartRecoveryRuns = plan . runs ;
7380 }
81+ if ( plan . forceRestartSafeTools ) {
82+ entry . restartRecoveryForceSafeTools = true ;
83+ }
7484 transitionMainSessionRecovery ( entry , {
7585 kind : "mark_interrupted" ,
7686 cycleId : randomUUID ( ) ,
@@ -121,7 +131,15 @@ export async function markRestartAbortedMainSessions(params: {
121131 . filter ( ( run ) => run . runId && run . lifecycleGeneration && ( run . sessionKey || run . sessionId ) ) ;
122132 const currentLifecycleGeneration = getAgentEventLifecycleGeneration ( ) ;
123133 const result = { marked : 0 , skipped : 0 } ;
124- if ( sessionKeys . size === 0 && sessionIds . size === 0 ) {
134+ // Channel work can outlive its chat-run registration. The admission owner
135+ // retains the authoritative store and session identities until the turn releases.
136+ const activeAdmissions = collectActiveSessionWorkAdmissions ( ) ;
137+ if (
138+ sessionKeys . size === 0 &&
139+ sessionIds . size === 0 &&
140+ activeRuns . length === 0 &&
141+ activeAdmissions . size === 0
142+ ) {
125143 return result ;
126144 }
127145
@@ -169,7 +187,11 @@ export async function markRestartAbortedMainSessions(params: {
169187 storePaths . add ( path . join ( sessionsDir , "sessions.json" ) ) ;
170188 }
171189
190+ for ( const storePath of activeAdmissions . keys ( ) ) {
191+ storePaths . add ( storePath ) ;
192+ }
172193 for ( const storePath of storePaths ) {
194+ const activeAdmissionIdentities = activeAdmissions . get ( storePath ) ?? new Set < string > ( ) ;
173195 const storeResult = await markRecoveryStore ( {
174196 storePath,
175197 plan : ( entry , sessionKey ) => {
@@ -190,14 +212,20 @@ export async function markRestartAbortedMainSessions(params: {
190212 if (
191213 entry . status !== "running" &&
192214 matchingActiveRuns . length === 0 &&
193- registeredActiveRuns . length === 0
215+ registeredActiveRuns . length === 0 &&
216+ ! activeAdmissionIdentities . has ( sessionKey ) &&
217+ ! activeAdmissionIdentities . has ( entry . sessionId )
194218 ) {
195219 return undefined ;
196220 }
221+ const matchedActiveAdmission =
222+ activeAdmissionIdentities . has ( sessionKey ) ||
223+ activeAdmissionIdentities . has ( entry . sessionId ) ;
197224 const matches =
198- typeof entry . sessionId === "string" && sessionIds . has ( entry . sessionId )
225+ matchedActiveAdmission ||
226+ ( typeof entry . sessionId === "string" && sessionIds . has ( entry . sessionId )
199227 ? true
200- : ! preferSessionIdMatch && sessionKeys . has ( sessionKey ) ;
228+ : ! preferSessionIdMatch && sessionKeys . has ( sessionKey ) ) ;
201229 if ( ! matches ) {
202230 return undefined ;
203231 }
@@ -212,7 +240,13 @@ export async function markRestartAbortedMainSessions(params: {
212240 lifecycleGeneration,
213241 } ) ) ,
214242 ] ) ;
215- return { action : "mark" , replaceRuns : true , resetRuntime : ! wasRunning , runs } ;
243+ return {
244+ action : "mark" ,
245+ forceRestartSafeTools : matchedActiveAdmission ,
246+ replaceRuns : true ,
247+ resetRuntime : ! wasRunning ,
248+ runs,
249+ } ;
216250 } ,
217251 } ) ;
218252 result . marked += storeResult . marked ;
0 commit comments