|
2 | 2 | import { AsyncResource } from "node:async_hooks"; |
3 | 3 | import { expectDefined } from "@openclaw/normalization-core"; |
4 | 4 | import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { |
| 6 | + clearActiveEmbeddedRun, |
| 7 | + setActiveEmbeddedRun, |
| 8 | +} from "../../agents/embedded-agent-runner/runs.js"; |
5 | 9 | import type { OpenClawConfig } from "../../config/config.js"; |
6 | 10 | import { setActivePluginRegistry } from "../../plugins/runtime.js"; |
7 | 11 | import { |
@@ -1222,6 +1226,136 @@ describe("dispatchReplyFromConfig", () => { |
1222 | 1226 | } |
1223 | 1227 | }); |
1224 | 1228 |
|
| 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 | + |
1225 | 1359 | it("clears stale active reply operations for terminal sessions and retries admission", async () => { |
1226 | 1360 | setNoAbort(); |
1227 | 1361 | const sessionKey = "agent:main:telegram:group:-1003774691294"; |
|
0 commit comments