|
1 | 1 | // Verifies sessions_spawn lifecycle hooks, cleanup, and completion announcements. |
| 2 | +import fs from "node:fs"; |
| 3 | +import os from "node:os"; |
| 4 | +import path from "node:path"; |
2 | 5 | import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
3 | 6 | import type { AgentRouteBinding } from "../config/types.agents.js"; |
| 7 | +import { openDurableWorkflowSqliteStore } from "../durable/sqlite-store.js"; |
| 8 | +import { |
| 9 | + DURABLE_AGENT_TURN_WORKFLOW_ID, |
| 10 | + DURABLE_SUBAGENT_RUN_WORKFLOW_ID, |
| 11 | +} from "../durable/workflow-ids.js"; |
4 | 12 | import { emitAgentEvent } from "../infra/agent-events.js"; |
5 | 13 | import { |
6 | 14 | testing as bundleMcpRuntimeTesting, |
@@ -317,6 +325,111 @@ describe("openclaw-tools: subagents (sessions_spawn lifecycle)", () => { |
317 | 325 | expect(childAgentCall?.timeoutMs).toBe(125_000); |
318 | 326 | }); |
319 | 327 |
|
| 328 | + it("maps successful agent.wait ok outcomes to durable succeeded child links", async () => { |
| 329 | + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-sessions-spawn-durable-")); |
| 330 | + const dbPath = path.join(dir, "state", "openclaw.sqlite"); |
| 331 | + const previousDurableWorkflows = process.env.OPENCLAW_DURABLE_WORKFLOWS; |
| 332 | + const previousStateDir = process.env.OPENCLAW_STATE_DIR; |
| 333 | + const parentSessionKey = "main"; |
| 334 | + const requesterRunId = "run_parent_ok"; |
| 335 | + let parentWorkflowRunId = ""; |
| 336 | + |
| 337 | + process.env.OPENCLAW_DURABLE_WORKFLOWS = "1"; |
| 338 | + process.env.OPENCLAW_STATE_DIR = dir; |
| 339 | + try { |
| 340 | + const setupStore = openDurableWorkflowSqliteStore({ path: dbPath }); |
| 341 | + try { |
| 342 | + parentWorkflowRunId = setupStore.createRun({ |
| 343 | + workflowId: DURABLE_AGENT_TURN_WORKFLOW_ID, |
| 344 | + status: "running", |
| 345 | + recoveryState: "running", |
| 346 | + idempotencyKey: requesterRunId, |
| 347 | + sourceType: "agent_turn", |
| 348 | + sourceRef: parentSessionKey, |
| 349 | + metadata: { sessionKey: parentSessionKey }, |
| 350 | + now: 100, |
| 351 | + }).workflowRunId; |
| 352 | + } finally { |
| 353 | + setupStore.close(); |
| 354 | + } |
| 355 | + |
| 356 | + const ctx = setupSessionsSpawnGatewayMock({ |
| 357 | + includeChatHistory: true, |
| 358 | + agentWaitResult: { status: "ok", startedAt: 1000, endedAt: 2000 }, |
| 359 | + }); |
| 360 | + const tool = await getSessionsSpawnTool({ |
| 361 | + agentSessionKey: "main", |
| 362 | + agentChannel: "whatsapp", |
| 363 | + requesterRunId, |
| 364 | + }); |
| 365 | + |
| 366 | + await executeSpawnAndExpectAccepted({ |
| 367 | + tool, |
| 368 | + callId: "call-durable-ok", |
| 369 | + cleanup: "keep", |
| 370 | + label: "durable-ok", |
| 371 | + }); |
| 372 | + |
| 373 | + const child = ctx.getChild(); |
| 374 | + if (!child.runId) { |
| 375 | + throw new Error("missing child runId"); |
| 376 | + } |
| 377 | + if (!child.sessionKey) { |
| 378 | + throw new Error("missing child sessionKey"); |
| 379 | + } |
| 380 | + await waitForSessionsSpawnEvent( |
| 381 | + "durable ok outcome", |
| 382 | + () => |
| 383 | + ctx.waitCalls.some((call) => call.runId === child.runId) && |
| 384 | + getLatestSubagentRunByChildSessionKey(child.sessionKey!)?.outcome?.status === "ok", |
| 385 | + ); |
| 386 | + await waitForRunCleanup(child.sessionKey); |
| 387 | + |
| 388 | + const assertStore = openDurableWorkflowSqliteStore({ path: dbPath }); |
| 389 | + try { |
| 390 | + const durableChild = assertStore |
| 391 | + .listRuns({ limit: 50 }) |
| 392 | + .find( |
| 393 | + (run) => |
| 394 | + run.workflowId === DURABLE_SUBAGENT_RUN_WORKFLOW_ID && |
| 395 | + run.idempotencyKey === child.runId, |
| 396 | + ); |
| 397 | + expect(durableChild).toMatchObject({ |
| 398 | + status: "succeeded", |
| 399 | + recoveryState: "terminal", |
| 400 | + parentWorkflowRunId, |
| 401 | + metadata: expect.objectContaining({ |
| 402 | + childSessionKey: child.sessionKey, |
| 403 | + status: "ok", |
| 404 | + }), |
| 405 | + }); |
| 406 | + const parentLink = assertStore.listParentLinks(durableChild!.workflowRunId)[0]; |
| 407 | + expect(parentLink).toMatchObject({ |
| 408 | + parentWorkflowRunId, |
| 409 | + status: "succeeded", |
| 410 | + metadata: expect.objectContaining({ |
| 411 | + childSessionKey: child.sessionKey, |
| 412 | + status: "ok", |
| 413 | + }), |
| 414 | + }); |
| 415 | + } finally { |
| 416 | + assertStore.close(); |
| 417 | + } |
| 418 | + } finally { |
| 419 | + if (previousDurableWorkflows === undefined) { |
| 420 | + delete process.env.OPENCLAW_DURABLE_WORKFLOWS; |
| 421 | + } else { |
| 422 | + process.env.OPENCLAW_DURABLE_WORKFLOWS = previousDurableWorkflows; |
| 423 | + } |
| 424 | + if (previousStateDir === undefined) { |
| 425 | + delete process.env.OPENCLAW_STATE_DIR; |
| 426 | + } else { |
| 427 | + process.env.OPENCLAW_STATE_DIR = previousStateDir; |
| 428 | + } |
| 429 | + fs.rmSync(dir, { recursive: true, force: true }); |
| 430 | + } |
| 431 | + }); |
| 432 | + |
320 | 433 | it("sessions_spawn retires bundle MCP runtime when run-mode cleanup completes", async () => { |
321 | 434 | let resumeAnnounceFlow: ((value: boolean) => void) | undefined; |
322 | 435 | let announceFlowStarted: (() => void) | undefined; |
|
0 commit comments