Skip to content

Commit ed6ee79

Browse files
fix(gateway): steer matching embedded turns (#119287)
1 parent 7a0a4c5 commit ed6ee79

2 files changed

Lines changed: 151 additions & 1 deletion

File tree

‎src/auto-reply/reply/dispatch-from-config.base.test-utils.ts‎

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
import { AsyncResource } from "node:async_hooks";
33
import { expectDefined } from "@openclaw/normalization-core";
44
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
5+
import {
6+
clearActiveEmbeddedRun,
7+
setActiveEmbeddedRun,
8+
} from "../../agents/embedded-agent-runner/runs.js";
59
import type { OpenClawConfig } from "../../config/config.js";
610
import { setActivePluginRegistry } from "../../plugins/runtime.js";
711
import {
@@ -1222,6 +1226,136 @@ describe("dispatchReplyFromConfig", () => {
12221226
}
12231227
});
12241228

1229+
it("lets Gateway-owned turns reach queue resolution while only an embedded run is active", async () => {
1230+
setNoAbort();
1231+
const sessionKey = "agent:main:main";
1232+
const sessionId = "active-embedded-session";
1233+
const activeHandle = {
1234+
queueMessage: vi.fn(async () => {}),
1235+
isStreaming: () => true,
1236+
isCompacting: () => false,
1237+
abort: vi.fn(),
1238+
};
1239+
setActiveEmbeddedRun(sessionId, activeHandle, sessionKey);
1240+
sessionStoreMocks.currentEntry = { sessionId, updatedAt: Date.now() };
1241+
const dispatcher = createDispatcher();
1242+
const replyResolver = vi.fn(async () => {
1243+
expect(replyRunRegistry.get(sessionKey)).toBeUndefined();
1244+
return undefined;
1245+
});
1246+
1247+
try {
1248+
const result = await dispatchReplyFromConfig({
1249+
ctx: buildTestCtx({
1250+
Provider: "webchat",
1251+
Surface: "webchat",
1252+
SessionKey: sessionKey,
1253+
BodyForAgent: "steer this active turn",
1254+
}),
1255+
cfg: emptyConfig,
1256+
dispatcher,
1257+
replyOptions: {
1258+
turnAdoptionLifecycle: {
1259+
onAdopted: async () => {},
1260+
onDeferred: vi.fn(),
1261+
onSettled: vi.fn(),
1262+
},
1263+
},
1264+
replyResolver,
1265+
});
1266+
1267+
expect(result).toMatchObject({
1268+
queuedFinal: true,
1269+
counts: { tool: 0, block: 0, final: 0 },
1270+
noVisibleReplyFallbackDelivered: true,
1271+
});
1272+
expect(replyResolver).toHaveBeenCalledTimes(1);
1273+
expect(replyRunRegistry.get(sessionKey)).toBeUndefined();
1274+
} finally {
1275+
clearActiveEmbeddedRun(sessionId, activeHandle, sessionKey);
1276+
}
1277+
});
1278+
1279+
it("keeps non-Gateway turns on normal admission while only an embedded run is active", async () => {
1280+
setNoAbort();
1281+
const sessionKey = "agent:main:telegram:direct:embedded-only";
1282+
const sessionId = "active-non-gateway-embedded-session";
1283+
const activeHandle = {
1284+
queueMessage: vi.fn(async () => {}),
1285+
isStreaming: () => true,
1286+
isCompacting: () => false,
1287+
abort: vi.fn(),
1288+
};
1289+
setActiveEmbeddedRun(sessionId, activeHandle, sessionKey);
1290+
sessionStoreMocks.currentEntry = { sessionId, updatedAt: Date.now() };
1291+
const replyResolver = vi.fn(async () => {
1292+
expect(replyRunRegistry.get(sessionKey)?.sessionId).toBe(sessionId);
1293+
return undefined;
1294+
});
1295+
1296+
try {
1297+
await dispatchReplyFromConfig({
1298+
ctx: buildTestCtx({
1299+
Provider: "telegram",
1300+
Surface: "telegram",
1301+
SessionKey: sessionKey,
1302+
BodyForAgent: "queue through normal admission",
1303+
}),
1304+
cfg: emptyConfig,
1305+
dispatcher: createDispatcher(),
1306+
replyResolver,
1307+
});
1308+
1309+
expect(replyResolver).toHaveBeenCalledTimes(1);
1310+
} finally {
1311+
clearActiveEmbeddedRun(sessionId, activeHandle, sessionKey);
1312+
}
1313+
});
1314+
1315+
it("keeps Gateway turns on normal admission when the embedded run belongs to an old session", async () => {
1316+
setNoAbort();
1317+
const sessionKey = "agent:main:main";
1318+
const staleSessionId = "stale-embedded-session";
1319+
const currentSessionId = "current-session";
1320+
const activeHandle = {
1321+
queueMessage: vi.fn(async () => {}),
1322+
isStreaming: () => true,
1323+
isCompacting: () => false,
1324+
abort: vi.fn(),
1325+
};
1326+
setActiveEmbeddedRun(staleSessionId, activeHandle, sessionKey);
1327+
sessionStoreMocks.currentEntry = { sessionId: currentSessionId, updatedAt: Date.now() };
1328+
const replyResolver = vi.fn(async () => {
1329+
expect(replyRunRegistry.get(sessionKey)?.sessionId).toBe(currentSessionId);
1330+
return undefined;
1331+
});
1332+
1333+
try {
1334+
await dispatchReplyFromConfig({
1335+
ctx: buildTestCtx({
1336+
Provider: "webchat",
1337+
Surface: "webchat",
1338+
SessionKey: sessionKey,
1339+
BodyForAgent: "start on the current session",
1340+
}),
1341+
cfg: emptyConfig,
1342+
dispatcher: createDispatcher(),
1343+
replyOptions: {
1344+
turnAdoptionLifecycle: {
1345+
onAdopted: async () => {},
1346+
onDeferred: vi.fn(),
1347+
onSettled: vi.fn(),
1348+
},
1349+
},
1350+
replyResolver,
1351+
});
1352+
1353+
expect(replyResolver).toHaveBeenCalledTimes(1);
1354+
} finally {
1355+
clearActiveEmbeddedRun(staleSessionId, activeHandle, sessionKey);
1356+
}
1357+
});
1358+
12251359
it("clears stale active reply operations for terminal sessions and retries admission", async () => {
12261360
setNoAbort();
12271361
const sessionKey = "agent:main:telegram:group:-1003774691294";

‎src/auto-reply/reply/dispatch-from-config.lifecycle.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import crypto from "node:crypto";
2+
import { resolveActiveEmbeddedRunSessionId } from "../../agents/embedded-agent-runner/run-state.js";
23
import { isRecoverableTerminalSessionStatus } from "../../config/sessions/terminal-status.js";
34
import type { SessionEntry } from "../../config/sessions/types.js";
45
import { logVerbose } from "../../globals.js";
@@ -157,12 +158,27 @@ export function createDispatchReplyOperationCoordinator(params: {
157158
params.operationSessionStoreEntry.entry?.sessionId ??
158159
crypto.randomUUID();
159160
const replyTurnKind = resolveReplyTurnKind(params.replyOptions);
161+
const activeReplyOperation = replyRunRegistry.get(params.dispatchOperationSessionKey);
162+
const activeEmbeddedSessionId = resolveActiveEmbeddedRunSessionId(
163+
params.dispatchOperationSessionKey,
164+
);
165+
const allowGatewayEmbeddedQueueResolution =
166+
replyTurnKind === "visible" &&
167+
params.replyOptions?.turnAdoptionLifecycle !== undefined &&
168+
activeReplyOperation === undefined &&
169+
activeEmbeddedSessionId === operationSessionId;
170+
if (allowGatewayEmbeddedQueueResolution) {
171+
// An embedded owner can outlive its reply-operation registration. Do not
172+
// create a competing operation for the same session before queue policy
173+
// gets a chance to steer the active backend.
174+
return { status: "ready" };
175+
}
160176
const allowActivePreDispatch = phase === "pre_dispatch" && replyTurnKind === "visible";
161177
const allowGatewayQueueResolution =
162178
phase === "dispatch" &&
163179
replyTurnKind === "visible" &&
164180
params.replyOptions?.turnAdoptionLifecycle !== undefined &&
165-
replyRunRegistry.get(params.dispatchOperationSessionKey) !== undefined;
181+
activeReplyOperation !== undefined;
166182
if (allowGatewayQueueResolution) {
167183
// Gateway turns need to reach getReplyFromConfig while the owner is active;
168184
// that layer applies the session's steer/followup/collect/drop policy.

0 commit comments

Comments
 (0)