|
2 | 2 | import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest"; |
3 | 3 | import { readToolApprovalReviews } from "../../lib/chat/tool-approval-reviews.ts"; |
4 | 4 | import { readPreparedActivity, summarizeToolGroup } from "../../lib/chat/tool-call-grouping.ts"; |
5 | | -import { extractToolCardsCached } from "../../lib/chat/tool-cards.ts"; |
| 5 | +import { extractToolCardsCached, resolveToolCardOutcome } from "../../lib/chat/tool-cards.ts"; |
6 | 6 | import { coalesceToolActivityMessages } from "./chat-tool-activity-coalesce.ts"; |
7 | 7 | import type { ToolStreamEntry } from "./tool-stream-contract.ts"; |
8 | 8 | import { buildToolStreamIdentity } from "./tool-stream-identity.ts"; |
@@ -46,106 +46,151 @@ afterAll(() => { |
46 | 46 | afterEach(() => vi.useRealTimers()); |
47 | 47 |
|
48 | 48 | describe("app-tool-stream approval lifecycle", () => { |
49 | | - it("keeps raw details while terminal prepared activity wins history and reconnect replay", () => { |
50 | | - const host = createHost({ chatRunId: "run-1" }); |
51 | | - const started = { |
52 | | - itemId: "tool:call", |
53 | | - toolCallId: "call", |
54 | | - kind: "tool", |
55 | | - name: "process", |
56 | | - title: "Check process", |
57 | | - phase: "start", |
58 | | - status: "running", |
59 | | - hideFromChannelProgress: true, |
60 | | - }; |
61 | | - const completed = { |
62 | | - ...started, |
63 | | - phase: "end", |
64 | | - status: "completed", |
65 | | - title: "Stop process", |
66 | | - hideFromChannelProgress: false, |
67 | | - }; |
68 | | - const completedEvent = Object.freeze({ ...completed, diagnostic: { source: "native-tool" } }); |
69 | | - handleAgentEvent( |
70 | | - host, |
71 | | - agentEvent("run-1", 1, "tool", { |
| 49 | + it.each([ |
| 50 | + { withResult: true, kind: "tool" }, |
| 51 | + { withResult: false, kind: "tool" }, |
| 52 | + { withResult: false, kind: "command" }, |
| 53 | + ])( |
| 54 | + "keeps terminal $kind activity through history and replay (result: $withResult)", |
| 55 | + ({ withResult, kind }) => { |
| 56 | + const host = createHost({ chatRunId: "run-1" }); |
| 57 | + const started = { |
| 58 | + itemId: "tool:call", |
72 | 59 | toolCallId: "call", |
| 60 | + kind, |
73 | 61 | name: "process", |
| 62 | + title: "Check process", |
74 | 63 | phase: "start", |
75 | | - args: { action: "poll" }, |
76 | | - }), |
77 | | - ); |
78 | | - handleAgentEvent(host, agentEvent("run-1", 2, "item", started)); |
| 64 | + status: "running", |
| 65 | + hideFromChannelProgress: true, |
| 66 | + }; |
| 67 | + const completed = { |
| 68 | + ...started, |
| 69 | + phase: "end", |
| 70 | + status: withResult ? "completed" : undefined, |
| 71 | + title: "Stop process", |
| 72 | + hideFromChannelProgress: false, |
| 73 | + }; |
| 74 | + const completedEvent = Object.freeze({ ...completed, diagnostic: { source: "native-tool" } }); |
| 75 | + handleAgentEvent( |
| 76 | + host, |
| 77 | + agentEvent("run-1", 1, "tool", { |
| 78 | + toolCallId: "call", |
| 79 | + name: "process", |
| 80 | + phase: "start", |
| 81 | + args: { action: "poll" }, |
| 82 | + }), |
| 83 | + ); |
| 84 | + handleAgentEvent(host, agentEvent("run-1", 2, "item", started)); |
| 85 | + if (withResult) { |
| 86 | + handleAgentEvent( |
| 87 | + host, |
| 88 | + agentEvent("run-1", 3, "tool", { |
| 89 | + toolCallId: "call", |
| 90 | + name: "process", |
| 91 | + phase: "result", |
| 92 | + isError: false, |
| 93 | + result: "raw execution result", |
| 94 | + }), |
| 95 | + ); |
| 96 | + } |
| 97 | + handleAgentEvent(host, agentEvent("run-1", 4, "item", completedEvent)); |
| 98 | + handleAgentEvent(host, agentEvent("run-1", 2, "item", started)); |
| 99 | + const live = [...host.toolStreamById.values()][0]!.message; |
| 100 | + const reconciled = coalesceToolActivityMessages([ |
| 101 | + { |
| 102 | + kind: "message", |
| 103 | + key: "history", |
| 104 | + message: { |
| 105 | + role: "assistant", |
| 106 | + runId: "run-1", |
| 107 | + __openclaw: { id: "history-call" }, |
| 108 | + content: [ |
| 109 | + { type: "toolCall", id: "call", name: "process", arguments: { action: "poll" } }, |
| 110 | + ], |
| 111 | + activity: [started], |
| 112 | + }, |
| 113 | + }, |
| 114 | + { kind: "message", key: "live", message: live }, |
| 115 | + ]); |
| 116 | + const messages = reconciled.flatMap((item) => |
| 117 | + item.kind === "message" ? [item.message] : [], |
| 118 | + ); |
| 119 | + const activity = messages.flatMap(readPreparedActivity); |
| 120 | + expect(activity).toEqual([completed]); |
| 121 | + expect(summarizeToolGroup(activity)).toBe( |
| 122 | + withResult ? "1 other operation" : "1 other operation · 1 unknown", |
| 123 | + ); |
| 124 | + expect(activity[0]).not.toHaveProperty("diagnostic"); |
| 125 | + expect(completedEvent.diagnostic).toEqual({ source: "native-tool" }); |
| 126 | + const cards = messages.flatMap(extractToolCardsCached); |
| 127 | + expect(cards).toMatchObject([{ args: { action: "poll" }, completed: true }]); |
| 128 | + expect(cards.map((card) => card.outputText)).toEqual([ |
| 129 | + withResult ? "raw execution result" : undefined, |
| 130 | + ]); |
| 131 | + expect(cards.map((card) => resolveToolCardOutcome(card, true))).toEqual([ |
| 132 | + withResult ? "succeeded" : "unknown", |
| 133 | + ]); |
| 134 | + expect(messages[0]).toMatchObject({ __openclawToolStreamResultReceived: withResult }); |
| 135 | + const quietHistory = coalesceToolActivityMessages([ |
| 136 | + { |
| 137 | + kind: "message", |
| 138 | + key: "call", |
| 139 | + message: { |
| 140 | + role: "assistant", |
| 141 | + runId: "run-1", |
| 142 | + activity: [started], |
| 143 | + content: [ |
| 144 | + { type: "toolCall", id: "call", name: "process", arguments: { action: "poll" } }, |
| 145 | + ], |
| 146 | + }, |
| 147 | + }, |
| 148 | + { |
| 149 | + kind: "message", |
| 150 | + key: "result", |
| 151 | + message: { |
| 152 | + role: "toolResult", |
| 153 | + runId: "run-1", |
| 154 | + toolCallId: "call", |
| 155 | + toolName: "process", |
| 156 | + isError: false, |
| 157 | + content: "raw wait result", |
| 158 | + activity: [], |
| 159 | + }, |
| 160 | + }, |
| 161 | + ]); |
| 162 | + expect( |
| 163 | + quietHistory.flatMap((item) => |
| 164 | + item.kind === "message" ? readPreparedActivity(item.message) : [], |
| 165 | + ), |
| 166 | + ).toEqual([]); |
| 167 | + resetToolStream(host); |
| 168 | + }, |
| 169 | + ); |
| 170 | + |
| 171 | + it("keeps suppressed native item completion from settling the visible call", () => { |
| 172 | + const host = createHost({ chatRunId: "run-1" }); |
| 173 | + emitTool(host, "run-1", 1, { |
| 174 | + toolCallId: "call", |
| 175 | + name: "exec", |
| 176 | + phase: "start", |
| 177 | + args: { command: "check" }, |
| 178 | + }); |
79 | 179 | handleAgentEvent( |
80 | 180 | host, |
81 | | - agentEvent("run-1", 3, "tool", { |
| 181 | + agentEvent("run-1", 2, "item", { |
| 182 | + itemId: "native-call", |
82 | 183 | toolCallId: "call", |
83 | | - name: "process", |
84 | | - phase: "result", |
85 | | - isError: false, |
86 | | - result: "raw execution result", |
| 184 | + kind: "command", |
| 185 | + name: "exec", |
| 186 | + title: "Check", |
| 187 | + phase: "end", |
| 188 | + suppressChannelProgress: true, |
87 | 189 | }), |
88 | 190 | ); |
89 | | - handleAgentEvent(host, agentEvent("run-1", 4, "item", completedEvent)); |
90 | | - handleAgentEvent(host, agentEvent("run-1", 2, "item", started)); |
91 | | - const live = [...host.toolStreamById.values()][0]!.message; |
92 | | - const reconciled = coalesceToolActivityMessages([ |
93 | | - { |
94 | | - kind: "message", |
95 | | - key: "history", |
96 | | - message: { |
97 | | - role: "assistant", |
98 | | - runId: "run-1", |
99 | | - __openclaw: { id: "history-call" }, |
100 | | - content: [ |
101 | | - { type: "toolCall", id: "call", name: "process", arguments: { action: "poll" } }, |
102 | | - ], |
103 | | - activity: [started], |
104 | | - }, |
105 | | - }, |
106 | | - { kind: "message", key: "live", message: live }, |
107 | | - ]); |
108 | | - const messages = reconciled.flatMap((item) => (item.kind === "message" ? [item.message] : [])); |
109 | | - const activity = messages.flatMap(readPreparedActivity); |
110 | | - expect(activity).toEqual([completed]); |
111 | | - expect(summarizeToolGroup(activity)).toBe("1 other operation"); |
112 | | - expect(activity[0]).not.toHaveProperty("diagnostic"); |
113 | | - expect(completedEvent.diagnostic).toEqual({ source: "native-tool" }); |
114 | | - expect(messages.flatMap(extractToolCardsCached)).toMatchObject([ |
115 | | - { args: { action: "poll" }, outputText: "raw execution result", completed: true }, |
116 | | - ]); |
117 | | - const quietHistory = coalesceToolActivityMessages([ |
118 | | - { |
119 | | - kind: "message", |
120 | | - key: "call", |
121 | | - message: { |
122 | | - role: "assistant", |
123 | | - runId: "run-1", |
124 | | - activity: [started], |
125 | | - content: [ |
126 | | - { type: "toolCall", id: "call", name: "process", arguments: { action: "poll" } }, |
127 | | - ], |
128 | | - }, |
129 | | - }, |
130 | | - { |
131 | | - kind: "message", |
132 | | - key: "result", |
133 | | - message: { |
134 | | - role: "toolResult", |
135 | | - runId: "run-1", |
136 | | - toolCallId: "call", |
137 | | - toolName: "process", |
138 | | - isError: false, |
139 | | - content: "raw wait result", |
140 | | - activity: [], |
141 | | - }, |
142 | | - }, |
143 | | - ]); |
144 | | - expect( |
145 | | - quietHistory.flatMap((item) => |
146 | | - item.kind === "message" ? readPreparedActivity(item.message) : [], |
147 | | - ), |
148 | | - ).toEqual([]); |
| 191 | + const [card] = extractToolCardsCached(host.chatToolMessages[0]); |
| 192 | + expect(resolveToolCardOutcome(card!, true)).toBe("running"); |
| 193 | + expect(card?.outputText).toBeUndefined(); |
149 | 194 | resetToolStream(host); |
150 | 195 | }); |
151 | 196 |
|
|
0 commit comments