|
| 1 | +import { promises as fs } from "node:fs"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 5 | +import { resolveCompletionFromCurrentRunTranscript } from "./subagent-session-reconciliation.js"; |
| 6 | + |
| 7 | +describe("subagent session reconciliation", () => { |
| 8 | + let tmpDir: string; |
| 9 | + |
| 10 | + beforeEach(async () => { |
| 11 | + tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-subagent-reconcile-")); |
| 12 | + }); |
| 13 | + |
| 14 | + afterEach(async () => { |
| 15 | + await fs.rm(tmpDir, { recursive: true, force: true }); |
| 16 | + }); |
| 17 | + |
| 18 | + async function writeTranscript(events: unknown[]): Promise<string> { |
| 19 | + const transcriptFile = path.join(tmpDir, "child-run.jsonl"); |
| 20 | + await fs.writeFile( |
| 21 | + transcriptFile, |
| 22 | + `${events.map((event) => JSON.stringify(event)).join("\n")}\n`, |
| 23 | + "utf-8", |
| 24 | + ); |
| 25 | + return transcriptFile; |
| 26 | + } |
| 27 | + |
| 28 | + it("recovers completion from the current run private terminal stop turn", async () => { |
| 29 | + const startedAt = Date.parse("2026-03-24T12:00:00Z"); |
| 30 | + const endedAt = startedAt + 1_234; |
| 31 | + const transcriptFile = await writeTranscript([ |
| 32 | + { type: "session", version: 1, id: "sess-child" }, |
| 33 | + { |
| 34 | + message: { |
| 35 | + role: "assistant", |
| 36 | + content: "stale copied answer", |
| 37 | + stopReason: "stop", |
| 38 | + timestamp: startedAt - 1, |
| 39 | + }, |
| 40 | + }, |
| 41 | + { |
| 42 | + message: { |
| 43 | + role: "assistant", |
| 44 | + content: [{ type: "text", text: "current child result" }], |
| 45 | + stopReason: "stop", |
| 46 | + timestamp: endedAt, |
| 47 | + }, |
| 48 | + }, |
| 49 | + ]); |
| 50 | + |
| 51 | + await expect( |
| 52 | + resolveCompletionFromCurrentRunTranscript({ |
| 53 | + childSessionKey: "agent:main:subagent:child", |
| 54 | + transcriptFile, |
| 55 | + fallbackEndedAt: endedAt + 10_000, |
| 56 | + notBeforeMs: startedAt, |
| 57 | + startedAt, |
| 58 | + }), |
| 59 | + ).resolves.toEqual({ |
| 60 | + startedAt, |
| 61 | + endedAt, |
| 62 | + outcome: { status: "ok" }, |
| 63 | + reason: "subagent-complete", |
| 64 | + resultText: "current child result", |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + it("does not recover stale copied output from before the current run", async () => { |
| 69 | + const startedAt = Date.parse("2026-03-24T12:00:00Z"); |
| 70 | + const transcriptFile = await writeTranscript([ |
| 71 | + { type: "session", version: 1, id: "sess-child" }, |
| 72 | + { |
| 73 | + message: { |
| 74 | + role: "assistant", |
| 75 | + content: "old child result", |
| 76 | + stopReason: "stop", |
| 77 | + timestamp: startedAt - 1, |
| 78 | + }, |
| 79 | + }, |
| 80 | + ]); |
| 81 | + |
| 82 | + await expect( |
| 83 | + resolveCompletionFromCurrentRunTranscript({ |
| 84 | + childSessionKey: "agent:main:subagent:child", |
| 85 | + transcriptFile, |
| 86 | + fallbackEndedAt: startedAt + 1_000, |
| 87 | + notBeforeMs: startedAt, |
| 88 | + startedAt, |
| 89 | + }), |
| 90 | + ).resolves.toBeNull(); |
| 91 | + }); |
| 92 | + |
| 93 | + it.each(["error", "aborted", "toolUse"])( |
| 94 | + "does not recover a non-success terminal turn with stopReason=%s", |
| 95 | + async (stopReason) => { |
| 96 | + const startedAt = Date.parse("2026-03-24T12:00:00Z"); |
| 97 | + const transcriptFile = await writeTranscript([ |
| 98 | + { type: "session", version: 1, id: "sess-child" }, |
| 99 | + { |
| 100 | + message: { |
| 101 | + role: "assistant", |
| 102 | + content: "partial child text", |
| 103 | + stopReason, |
| 104 | + timestamp: startedAt + 1, |
| 105 | + }, |
| 106 | + }, |
| 107 | + ]); |
| 108 | + |
| 109 | + await expect( |
| 110 | + resolveCompletionFromCurrentRunTranscript({ |
| 111 | + childSessionKey: "agent:main:subagent:child", |
| 112 | + transcriptFile, |
| 113 | + fallbackEndedAt: startedAt + 10_000, |
| 114 | + notBeforeMs: startedAt, |
| 115 | + startedAt, |
| 116 | + }), |
| 117 | + ).resolves.toBeNull(); |
| 118 | + }, |
| 119 | + ); |
| 120 | +}); |
0 commit comments