Skip to content

Commit 8e40753

Browse files
fix(gateway): recover embedded channel runs after reload (#123189)
Project channel-originated embedded run identity and bounded progress through Gateway history so Control UI keeps the original timer, safe recent activity, and exact Stop ownership after reload. Co-authored-by: SunnyShu0925 <[email protected]> Co-authored-by: Ayaan Zaidi <[email protected]>
1 parent 23ae7bc commit 8e40753

25 files changed

Lines changed: 716 additions & 73 deletions

‎extensions/codex/src/app-server/run-attempt-active-turn.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ export async function activateCodexAttemptTurn(
334334
const handle = {
335335
kind: "embedded" as const,
336336
runId: params.runId,
337+
startedAtMs: params.startedAtMs,
337338
toolAuthorityFingerprint: params.toolAuthorityFingerprint,
338339
claimPendingUserInputAnswer,
339340
cancelPendingUserInput,

‎extensions/codex/src/app-server/run-attempt.steering.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,27 @@ describe("runCodexAppServerAttempt steering", () => {
151151
});
152152
});
153153

154+
it("threads the core run start time onto the active-turn handle", async () => {
155+
const { waitForMethod, completeTurn } = createStartedThreadHarness();
156+
const startedAtMs = 1_750_000_000_000;
157+
const params = { ...createSteeringParams(), startedAtMs };
158+
activeRunRegistrationMocks.setActiveEmbeddedRun.mockClear();
159+
const run = runCodexAppServerAttempt(params);
160+
await waitForMethod("turn/start");
161+
162+
let handle: { startedAtMs?: number } | undefined;
163+
await vi.waitFor(() => {
164+
handle = activeRunRegistrationMocks.setActiveEmbeddedRun.mock.calls.findLast(
165+
(call) => call[0] === params.sessionId,
166+
)?.[1] as typeof handle;
167+
expect(handle).toBeDefined();
168+
}, fastWait);
169+
expect(handle?.startedAtMs).toBe(startedAtMs);
170+
171+
await completeTurn({ threadId: "thread-1", turnId: "turn-1" });
172+
await run;
173+
});
174+
154175
it("exposes pending-question cancellation for queued image fallback", async () => {
155176
const harness = createStartedThreadHarness();
156177
const params = createSteeringParams();

‎extensions/copilot/src/attempt-active-run.test.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ vi.mock("openclaw/plugin-sdk/agent-harness-runtime", async (importOriginal) => {
2424

2525
function registerTestRun(params?: {
2626
canAcceptSteering?: () => boolean;
27+
startedAtMs?: number;
2728
receipt?: Promise<void>;
2829
send?: SessionLike["send"];
2930
}) {
@@ -40,6 +41,7 @@ function registerTestRun(params?: {
4041
abortActiveSession: vi.fn(),
4142
bridge: undefined,
4243
canAcceptSteering: params?.canAcceptSteering ?? (() => true),
44+
startedAtMs: params?.startedAtMs ?? 1_750_000_000_000,
4345
input: { runId: "run-1", sessionId: "session-1" } as AttemptParamsLike,
4446
isAborted: () => false,
4547
isSettled: () => false,
@@ -156,3 +158,9 @@ describe("registerCopilotActiveRun", () => {
156158
expect(onQueueAccepted).toHaveBeenCalledOnce();
157159
});
158160
});
161+
162+
it("threads the attempt start timestamp onto the embedded run handle", () => {
163+
const startedAtMs = 1_750_000_000_000;
164+
const { handle } = registerTestRun({ startedAtMs });
165+
expect(handle.startedAtMs).toBe(startedAtMs);
166+
});

‎extensions/copilot/src/attempt-active-run.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export function registerCopilotActiveRun(params: {
1717
abortActiveSession: () => void;
1818
bridge: ReturnType<typeof attachEventBridge> | undefined;
1919
canAcceptSteering: () => boolean;
20+
startedAtMs?: number;
2021
input: AttemptParamsLike;
2122
isAborted: () => boolean;
2223
isSettled: () => boolean;
@@ -102,6 +103,7 @@ export function registerCopilotActiveRun(params: {
102103
const activeRunHandle = {
103104
kind: "embedded" as const,
104105
runId: params.input.runId,
106+
startedAtMs: params.startedAtMs,
105107
toolAuthorityFingerprint: params.input.toolAuthorityFingerprint,
106108
claimPendingUserInputAnswer,
107109
cancelPendingUserInput,

‎extensions/copilot/src/attempt-execution.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ export async function runCopilotExecution(context: {
474474
abortActiveSession,
475475
bridge,
476476
canAcceptSteering: () => initialSdkUserValidated,
477+
startedAtMs: input.startedAtMs,
477478
input,
478479
isAborted: () => aborted,
479480
isSettled: () => settled,

‎src/agents/embedded-agent-runner/run-state.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ export type EmbeddedAgentQueueHandle = {
3434
readonly diagnosticOwner?: DiagnosticEmbeddedRunOwner;
3535
/** Synchronously closes diagnostic authority before this handle is evicted. */
3636
readonly closeDiagnostics?: () => void;
37+
/** Core run start time used by live recovery projections. */
38+
startedAtMs?: number;
3739
/** Exact authority of the concrete provider/model attempt behind this handle. */
3840
toolAuthorityFingerprint?: string;
3941
/** Atomically consumes one plain-text answer for this run's pending user-input request. */
@@ -75,6 +77,11 @@ export type ActiveEmbeddedRunSnapshot = {
7577
inFlightPrompt?: string;
7678
};
7779

80+
export type EmbeddedRunRegistration = {
81+
sessionId: string;
82+
sessionKey?: string;
83+
};
84+
7885
export type EmbeddedRunWaiter = {
7986
resolve: (ended: boolean) => void;
8087
handle?: EmbeddedAgentQueueHandle;
@@ -94,6 +101,7 @@ const EMBEDDED_RUN_STATE_KEY = Symbol.for("openclaw.embeddedRunState");
94101
const embeddedRunState = resolveGlobalSingleton(EMBEDDED_RUN_STATE_KEY, () => ({
95102
activeRuns: new Map<string, EmbeddedAgentQueueHandle>(),
96103
activeRunsByRunId: new Map<string, EmbeddedAgentQueueHandle>(),
104+
activeRunRegistrations: new WeakMap<EmbeddedAgentQueueHandle, EmbeddedRunRegistration>(),
97105
activeRunLifecycleGenerations: new WeakMap<EmbeddedAgentQueueHandle, string>(),
98106
retainedAbortabilityRunIds: new Set<string>(),
99107
snapshots: new Map<string, ActiveEmbeddedRunSnapshot>(),
@@ -113,6 +121,12 @@ export const ACTIVE_EMBEDDED_RUNS =
113121
export const ACTIVE_EMBEDDED_RUNS_BY_RUN_ID =
114122
embeddedRunState.activeRunsByRunId ??
115123
(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+
>());
116130
const ACTIVE_EMBEDDED_RUN_LIFECYCLE_GENERATIONS =
117131
embeddedRunState.activeRunLifecycleGenerations ??
118132
(embeddedRunState.activeRunLifecycleGenerations = new WeakMap<

‎src/agents/embedded-agent-runner/run/attempt-dispatch-preparation.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ export async function prepareAndDispatchEmbeddedRunAttempt(input: {
222222
});
223223
const dispatchedAttempt = await dispatchEmbeddedRunAttempt({
224224
params,
225+
runStartedAtMs: runInput.startedAtMs,
225226
transcriptOwnership: params.sessionManager
226227
? { kind: "caller-owned", sessionManager: params.sessionManager }
227228
: { kind: "runtime-target", sessionTarget: resolvedSessionTarget },

‎src/agents/embedded-agent-runner/run/attempt-stream-prepare.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ export function prepareEmbeddedAttemptStream(input: {
480480
runId: attempt.runId,
481481
diagnosticOwner: input.diagnosticOwner,
482482
closeDiagnostics: () => closeDiagnosticEmbeddedRunOwner(input.diagnosticOwner),
483+
startedAtMs: attempt.startedAtMs,
483484
...(attempt.toolAuthorityFingerprint
484485
? { toolAuthorityFingerprint: attempt.toolAuthorityFingerprint }
485486
: {}),

‎src/agents/embedded-agent-runner/run/run-attempt-dispatch.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ type AttemptControl = {
116116

117117
export async function dispatchEmbeddedRunAttempt(input: {
118118
params: InternalRunParams;
119+
/** Run-owned start timestamp captured before admission; projected on recovery. */
120+
runStartedAtMs: number;
119121
runtime: AttemptRuntime;
120122
transcriptOwnership: AttemptTranscriptOwnership;
121123
control: AttemptControl;
@@ -269,6 +271,7 @@ export async function dispatchEmbeddedRunAttempt(input: {
269271
});
270272
const attemptParams: EmbeddedRunAttemptParams = {
271273
admittedRunContext: params.admittedRunContext,
274+
startedAtMs: input.runStartedAtMs,
272275
contextEngineAgentId: runtime.contextEngineAgentId,
273276
...(control.pluginHarnessOwnsTransport ? { sandbox: pluginSandbox } : {}),
274277
operation: "attempt",

‎src/agents/embedded-agent-runner/run/types.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ export type EmbeddedRunAttemptTrajectoryRecorder = {
9999

100100
export type EmbeddedRunAttemptParams = EmbeddedRunAttemptBase & {
101101
admittedRunContext: NonNullable<RunEmbeddedAgentParams["admittedRunContext"]>;
102+
/**
103+
* Run-owned start timestamp captured by the embedded-run orchestrator before
104+
* admission. Flows onto the queue handle so recovery can project the active
105+
* run's authoritative start time instead of the session's subagent first-run.
106+
*/
107+
startedAtMs?: number;
102108
/** Explicit session owner captured before fallback agent resolution. */
103109
contextEngineAgentId?: string;
104110
/** Host-resolved sandbox snapshot for plugin harness tool construction. */

0 commit comments

Comments
 (0)