@@ -14,11 +14,9 @@ import {
1414 type ReplyBackendQueueMessageResult ,
1515 type ReplyBackendMessageInjection ,
1616} from "../../auto-reply/reply/reply-run-registry.js" ;
17- import type { ChatRunPlanSnapshot } from "../../gateway/server-chat-state.js" ;
1817import {
1918 isAgentEventLifecycleGenerationCurrent ,
2019 registerAgentEventLifecycleRotationHandler ,
21- type AgentEventPayload ,
2220} from "../../infra/agent-events.js" ;
2321import type { DiagnosticEmbeddedRunOwner } from "../../logging/diagnostic-run-activity.js" ;
2422import { resolveGlobalSingleton } from "../../shared/global-singleton.js" ;
@@ -36,7 +34,7 @@ export type EmbeddedAgentQueueHandle = {
3634 readonly diagnosticOwner ?: DiagnosticEmbeddedRunOwner ;
3735 /** Synchronously closes diagnostic authority before this handle is evicted. */
3836 readonly closeDiagnostics ?: ( ) => void ;
39- /** Epoch ms when the embedded run started (authoritative, vs SessionEntry.startedAt which is the subagent first-run time) . */
37+ /** Core run start time used by live recovery projections . */
4038 startedAtMs ?: number ;
4139 /** Exact authority of the concrete provider/model attempt behind this handle. */
4240 toolAuthorityFingerprint ?: string ;
@@ -77,12 +75,11 @@ export type ActiveEmbeddedRunSnapshot = {
7775 transcriptLeafId : string | null ;
7876 messages ?: unknown [ ] ;
7977 inFlightPrompt ?: string ;
80- /** Bounded agent events for Control UI replay after reload. */
81- events ?: AgentEventPayload [ ] ;
82- /** Running byte size of `events`; trims oldest entries to stay within budget. */
83- byteLength ?: number ;
84- /** Latest plan snapshot for Control UI recovery after reload. */
85- plan ?: ChatRunPlanSnapshot ;
78+ } ;
79+
80+ export type EmbeddedRunRegistration = {
81+ sessionId : string ;
82+ sessionKey ?: string ;
8683} ;
8784
8885export type EmbeddedRunWaiter = {
@@ -104,10 +101,10 @@ const EMBEDDED_RUN_STATE_KEY = Symbol.for("openclaw.embeddedRunState");
104101const embeddedRunState = resolveGlobalSingleton ( EMBEDDED_RUN_STATE_KEY , ( ) => ( {
105102 activeRuns : new Map < string , EmbeddedAgentQueueHandle > ( ) ,
106103 activeRunsByRunId : new Map < string , EmbeddedAgentQueueHandle > ( ) ,
104+ activeRunRegistrations : new WeakMap < EmbeddedAgentQueueHandle , EmbeddedRunRegistration > ( ) ,
107105 activeRunLifecycleGenerations : new WeakMap < EmbeddedAgentQueueHandle , string > ( ) ,
108106 retainedAbortabilityRunIds : new Set < string > ( ) ,
109107 snapshots : new Map < string , ActiveEmbeddedRunSnapshot > ( ) ,
110- eventSubscriptionsByRunId : new Map < string , ( ) => void > ( ) ,
111108 sessionIdsByKey : new Map < string , string > ( ) ,
112109 sessionIdsByFile : new Map < string , string > ( ) ,
113110 abandonedRunsBySessionId : new Map < string , AbandonedEmbeddedRun > ( ) ,
@@ -124,6 +121,12 @@ export const ACTIVE_EMBEDDED_RUNS =
124121export const ACTIVE_EMBEDDED_RUNS_BY_RUN_ID =
125122 embeddedRunState . activeRunsByRunId ??
126123 ( embeddedRunState . activeRunsByRunId = new Map < string , EmbeddedAgentQueueHandle > ( ) ) ;
124+ export const ACTIVE_EMBEDDED_RUN_REGISTRATIONS =
125+ embeddedRunState . activeRunRegistrations ??
126+ ( embeddedRunState . activeRunRegistrations = new WeakMap <
127+ EmbeddedAgentQueueHandle ,
128+ EmbeddedRunRegistration
129+ > ( ) ) ;
127130const ACTIVE_EMBEDDED_RUN_LIFECYCLE_GENERATIONS =
128131 embeddedRunState . activeRunLifecycleGenerations ??
129132 ( embeddedRunState . activeRunLifecycleGenerations = new WeakMap <
@@ -136,9 +139,6 @@ export const RETAINED_EMBEDDED_RUN_ABORTABILITY_RUN_IDS =
136139export const ACTIVE_EMBEDDED_RUN_SNAPSHOTS =
137140 embeddedRunState . snapshots ??
138141 ( embeddedRunState . snapshots = new Map < string , ActiveEmbeddedRunSnapshot > ( ) ) ;
139- export const EMBEDDED_RUN_EVENT_SUBSCRIPTIONS_BY_RUN_ID =
140- embeddedRunState . eventSubscriptionsByRunId ??
141- ( embeddedRunState . eventSubscriptionsByRunId = new Map < string , ( ) => void > ( ) ) ;
142142export const ACTIVE_EMBEDDED_RUN_SESSION_IDS_BY_KEY =
143143 embeddedRunState . sessionIdsByKey ??
144144 ( embeddedRunState . sessionIdsByKey = new Map < string , string > ( ) ) ;
@@ -190,10 +190,6 @@ function evictPriorLifecycleEmbeddedRuns(): void {
190190 if ( ACTIVE_EMBEDDED_RUNS_BY_RUN_ID . get ( runId ) === handle ) {
191191 ACTIVE_EMBEDDED_RUNS_BY_RUN_ID . delete ( runId ) ;
192192 RETAINED_EMBEDDED_RUN_ABORTABILITY_RUN_IDS . delete ( runId ) ;
193- // Release the stale run's event subscription so its listener does not
194- // survive lifecycle rotation for the process lifetime.
195- EMBEDDED_RUN_EVENT_SUBSCRIPTIONS_BY_RUN_ID . get ( runId ) ?.( ) ;
196- EMBEDDED_RUN_EVENT_SUBSCRIPTIONS_BY_RUN_ID . delete ( runId ) ;
197193 }
198194 }
199195 for ( const [ sessionKey , sessionId ] of ACTIVE_EMBEDDED_RUN_SESSION_IDS_BY_KEY ) {
0 commit comments