@wherever-dev/client 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,294 @@
1
+ import {describe, it, expect, beforeEach, afterEach, vi} from 'vitest';
2
+ import {get} from 'sveltore';
3
+ import {WhereverClient} from '../src/client.js';
4
+ import type {WhereverState} from '../src/types.js';
5
+ import {makeWSFactory} from './harness.js';
6
+
7
+ // A tool call with NO matching tool_result in loaded history is ambiguous:
8
+ // - if the session is streaming (live tail), the tool is STILL RUNNING;
9
+ // - otherwise the call is FROZEN with no result, i.e. it was interrupted
10
+ // (e.g. a CLI took over the session and killed the running tool).
11
+ // The frozen case must be flagged `interrupted` so the UI shows a neutral
12
+ // "no result" state instead of a bogus green success tick.
13
+
14
+ function makeClient(ws: ReturnType<typeof makeWSFactory>) {
15
+ const client = new WhereverClient({
16
+ host: 'localhost',
17
+ port: 1234,
18
+ secure: false,
19
+ WebSocketCtor: ws.ctor,
20
+ });
21
+ client.connect();
22
+ ws.last().open();
23
+ client.joinSession('/tmp/project/session.jsonl');
24
+ return client;
25
+ }
26
+
27
+ describe('interrupted (result-less) tool call from loaded history', () => {
28
+ let ws: ReturnType<typeof makeWSFactory>;
29
+ let client: WhereverClient;
30
+ const state = () => get<WhereverState>(client.stateStore);
31
+
32
+ beforeEach(() => {
33
+ vi.useFakeTimers();
34
+ ws = makeWSFactory();
35
+ client = makeClient(ws);
36
+ });
37
+
38
+ afterEach(() => {
39
+ client.disconnect(true);
40
+ vi.useRealTimers();
41
+ });
42
+
43
+ it('marks a dangling tool_call as interrupted when the session is NOT streaming', () => {
44
+ ws.last().receive({
45
+ type: 'session_created',
46
+ sessionId: 'sid-1',
47
+ sessionFile: '/tmp/project/session.jsonl',
48
+ cwd: '/tmp/project',
49
+ model: 'fake:model',
50
+ isStreaming: false,
51
+ });
52
+ ws.last().receive({
53
+ type: 'message_history',
54
+ sessionId: 'sid-1',
55
+ messages: [
56
+ {role: 'user', content: 'go', timestamp: 1_000},
57
+ {
58
+ role: 'tool_call',
59
+ content: '{"command":"sleep 10"}',
60
+ timestamp: 10_000,
61
+ toolName: 'bash',
62
+ },
63
+ ],
64
+ totalCount: 2,
65
+ offset: 0,
66
+ });
67
+
68
+ const tool = state().messages.find((m) => m.role === 'tool')!;
69
+ expect(tool.interrupted).toBe(true);
70
+ expect(tool.isStreaming).toBe(false);
71
+ expect(tool.isError).toBeFalsy();
72
+ // No result: outcome unknown, so no end timestamp either.
73
+ expect(tool.endedAt).toBeUndefined();
74
+ });
75
+
76
+ it('does NOT mark it interrupted when the session IS streaming (still running)', () => {
77
+ ws.last().receive({
78
+ type: 'session_created',
79
+ sessionId: 'sid-1',
80
+ sessionFile: '/tmp/project/session.jsonl',
81
+ cwd: '/tmp/project',
82
+ model: 'fake:model',
83
+ isStreaming: true,
84
+ });
85
+ ws.last().receive({
86
+ type: 'message_history',
87
+ sessionId: 'sid-1',
88
+ messages: [
89
+ {role: 'user', content: 'go', timestamp: 1_000},
90
+ {
91
+ role: 'tool_call',
92
+ content: '{"command":"sleep 10"}',
93
+ timestamp: 10_000,
94
+ toolName: 'bash',
95
+ },
96
+ ],
97
+ totalCount: 2,
98
+ offset: 0,
99
+ });
100
+
101
+ const tool = state().messages.find((m) => m.role === 'tool')!;
102
+ expect(tool.interrupted).toBeFalsy();
103
+ expect(tool.isStreaming).toBe(true);
104
+ });
105
+
106
+ it('renders a dangling mid-conversation tool_call IN PLACE, not hoisted to the end', () => {
107
+ // Recoverability case: an interrupted long-running bash call, superseded by
108
+ // a new user turn and later assistant replies. The dangling call must stay
109
+ // where it was issued (between "start it" and "never mind"), NOT be moved
110
+ // below the final reply as a phantom trailing "aborted tool call".
111
+ ws.last().receive({
112
+ type: 'session_created',
113
+ sessionId: 'sid-1',
114
+ sessionFile: '/tmp/project/session.jsonl',
115
+ cwd: '/tmp/project',
116
+ model: 'fake:model',
117
+ isStreaming: false,
118
+ });
119
+ ws.last().receive({
120
+ type: 'message_history',
121
+ sessionId: 'sid-1',
122
+ messages: [
123
+ {role: 'user', content: 'start it', timestamp: 1_000},
124
+ {
125
+ role: 'tool_call',
126
+ content: '{"command":"sleep 300"}',
127
+ timestamp: 2_000,
128
+ toolName: 'bash',
129
+ },
130
+ {role: 'user', content: 'never mind', timestamp: 3_000},
131
+ {role: 'assistant', content: 'ok, stopped', timestamp: 4_000},
132
+ {role: 'user', content: 'hello', timestamp: 5_000},
133
+ {role: 'assistant', content: 'hi there', timestamp: 6_000},
134
+ ],
135
+ totalCount: 6,
136
+ offset: 0,
137
+ });
138
+
139
+ const roles = state().messages.map((m) => m.role);
140
+ // The tool sits at index 1 (right after the first user message), not last.
141
+ expect(roles).toEqual([
142
+ 'user',
143
+ 'tool',
144
+ 'user',
145
+ 'assistant',
146
+ 'user',
147
+ 'assistant',
148
+ ]);
149
+ const tool = state().messages[1];
150
+ expect(tool.interrupted).toBe(true);
151
+ expect(tool.isStreaming).toBe(false);
152
+ // The final message is the assistant reply, NOT a trailing tool call.
153
+ expect(state().messages[state().messages.length - 1].role).toBe(
154
+ 'assistant',
155
+ );
156
+ });
157
+
158
+ it('with multiple dangling calls, only the newest streams on the live tail; earlier ones are interrupted in place', () => {
159
+ ws.last().receive({
160
+ type: 'session_created',
161
+ sessionId: 'sid-1',
162
+ sessionFile: '/tmp/project/session.jsonl',
163
+ cwd: '/tmp/project',
164
+ model: 'fake:model',
165
+ isStreaming: true,
166
+ });
167
+ ws.last().receive({
168
+ type: 'message_history',
169
+ sessionId: 'sid-1',
170
+ messages: [
171
+ {
172
+ role: 'tool_call',
173
+ content: '{"command":"sleep 1"}',
174
+ timestamp: 1_000,
175
+ toolName: 'bash',
176
+ },
177
+ {role: 'user', content: 'again', timestamp: 2_000},
178
+ {
179
+ role: 'tool_call',
180
+ content: '{"command":"sleep 2"}',
181
+ timestamp: 3_000,
182
+ toolName: 'bash',
183
+ },
184
+ ],
185
+ totalCount: 3,
186
+ offset: 0,
187
+ });
188
+
189
+ const tools = state().messages.filter((m) => m.role === 'tool');
190
+ expect(tools).toHaveLength(2);
191
+ // First (older) dangling call: interrupted, not streaming.
192
+ expect(tools[0].interrupted).toBe(true);
193
+ expect(tools[0].isStreaming).toBe(false);
194
+ // Newest dangling call: still running on the live tail.
195
+ expect(tools[1].interrupted).toBeFalsy();
196
+ expect(tools[1].isStreaming).toBe(true);
197
+ });
198
+
199
+ it('pairs a tool_result to its exact call by id, leaving the correct earlier same-named call dangling', () => {
200
+ // Two bash calls issued back-to-back; only the SECOND gets a result. With
201
+ // tool-call ids the result must resolve call #2 (by id), leaving call #1
202
+ // dangling/interrupted. Name-FIFO alone would wrongly resolve call #1 and
203
+ // leave call #2 dangling.
204
+ ws.last().receive({
205
+ type: 'session_created',
206
+ sessionId: 'sid-1',
207
+ sessionFile: '/tmp/project/session.jsonl',
208
+ cwd: '/tmp/project',
209
+ model: 'fake:model',
210
+ isStreaming: false,
211
+ });
212
+ ws.last().receive({
213
+ type: 'message_history',
214
+ sessionId: 'sid-1',
215
+ messages: [
216
+ {
217
+ role: 'tool_call',
218
+ content: '{"command":"sleep 300"}',
219
+ timestamp: 1_000,
220
+ toolName: 'bash',
221
+ toolCallId: 'call-A',
222
+ },
223
+ {
224
+ role: 'tool_call',
225
+ content: '{"command":"echo hi"}',
226
+ timestamp: 1_100,
227
+ toolName: 'bash',
228
+ toolCallId: 'call-B',
229
+ },
230
+ {
231
+ role: 'tool_result',
232
+ content: 'hi',
233
+ timestamp: 1_200,
234
+ toolName: 'bash',
235
+ toolCallId: 'call-B',
236
+ isError: false,
237
+ },
238
+ ],
239
+ totalCount: 3,
240
+ offset: 0,
241
+ });
242
+
243
+ const tools = state().messages.filter((m) => m.role === 'tool');
244
+ expect(tools).toHaveLength(2);
245
+ // Call A (sleep 300) is the dangling/interrupted one.
246
+ const callA = tools.find((t) => t.toolArgs?.includes('sleep 300'))!;
247
+ const callB = tools.find((t) => t.toolArgs?.includes('echo hi'))!;
248
+ expect(callA.interrupted).toBe(true);
249
+ expect(callA.toolOutput).toBe('');
250
+ // Call B got its result.
251
+ expect(callB.interrupted).toBeFalsy();
252
+ expect(callB.toolOutput).toBe('hi');
253
+ // Order in the transcript is preserved (A before B).
254
+ expect(state().messages.indexOf(callA)).toBeLessThan(
255
+ state().messages.indexOf(callB),
256
+ );
257
+ });
258
+
259
+ it('a matched tool_call + tool_result is NOT interrupted', () => {
260
+ ws.last().receive({
261
+ type: 'session_created',
262
+ sessionId: 'sid-1',
263
+ sessionFile: '/tmp/project/session.jsonl',
264
+ cwd: '/tmp/project',
265
+ model: 'fake:model',
266
+ isStreaming: false,
267
+ });
268
+ ws.last().receive({
269
+ type: 'message_history',
270
+ sessionId: 'sid-1',
271
+ messages: [
272
+ {
273
+ role: 'tool_call',
274
+ content: '{"command":"ls"}',
275
+ timestamp: 10_000,
276
+ toolName: 'bash',
277
+ },
278
+ {
279
+ role: 'tool_result',
280
+ content: 'a\nb',
281
+ timestamp: 11_000,
282
+ toolName: 'bash',
283
+ isError: false,
284
+ },
285
+ ],
286
+ totalCount: 2,
287
+ offset: 0,
288
+ });
289
+
290
+ const tool = state().messages.find((m) => m.role === 'tool')!;
291
+ expect(tool.interrupted).toBeFalsy();
292
+ expect(tool.isError).toBe(false);
293
+ });
294
+ });