@wherever-dev/client 0.3.0 → 0.3.1
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.
- package/CHANGELOG.md +16 -0
- package/dist/client.d.ts +11 -0
- package/dist/client.js +289 -14
- package/dist/client.js.map +1 -1
- package/dist/types.d.ts +3 -0
- package/package.json +1 -1
- package/src/client.ts +301 -15
- package/src/types.ts +20 -0
- package/test/message-delivery-confirmation.test.ts +194 -0
- package/test/mid-stream-steer.test.ts +91 -0
- package/test/tool-duration-history.test.ts +93 -0
- package/test/tool-duration.test.ts +86 -0
|
@@ -0,0 +1,194 @@
|
|
|
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
|
+
// The half-open loss: the socket reports OPEN, ws.send() succeeds (buffers, no
|
|
8
|
+
// throw), but the frame never reaches the server. The optimistic user echo must
|
|
9
|
+
// NOT be treated as delivered: it stays unconfirmed, is recoverable on reload,
|
|
10
|
+
// and flips to a "failed / needs retry" state instead of vanishing.
|
|
11
|
+
|
|
12
|
+
// A localStorage shim so persistence works under the node test env.
|
|
13
|
+
function installLocalStorage() {
|
|
14
|
+
const store = new Map<string, string>();
|
|
15
|
+
(globalThis as any).localStorage = {
|
|
16
|
+
getItem: (k: string) => (store.has(k) ? store.get(k)! : null),
|
|
17
|
+
setItem: (k: string, v: string) => void store.set(k, v),
|
|
18
|
+
removeItem: (k: string) => void store.delete(k),
|
|
19
|
+
clear: () => store.clear(),
|
|
20
|
+
};
|
|
21
|
+
return store;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function createAndLoad(
|
|
25
|
+
ws: ReturnType<typeof makeWSFactory>,
|
|
26
|
+
client: WhereverClient,
|
|
27
|
+
messages: any[] = [],
|
|
28
|
+
) {
|
|
29
|
+
client.joinSession('/tmp/project/session.jsonl');
|
|
30
|
+
ws.last().receive({
|
|
31
|
+
type: 'session_created',
|
|
32
|
+
sessionId: 'sid-1',
|
|
33
|
+
sessionFile: '/tmp/project/session.jsonl',
|
|
34
|
+
cwd: '/tmp/project',
|
|
35
|
+
model: 'fake:model',
|
|
36
|
+
isStreaming: false,
|
|
37
|
+
});
|
|
38
|
+
ws.last().receive({
|
|
39
|
+
type: 'message_history',
|
|
40
|
+
sessionId: 'sid-1',
|
|
41
|
+
messages,
|
|
42
|
+
totalCount: messages.length,
|
|
43
|
+
offset: 0,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
describe('outbound message delivery confirmation', () => {
|
|
48
|
+
let ws: ReturnType<typeof makeWSFactory>;
|
|
49
|
+
let client: WhereverClient;
|
|
50
|
+
const state = () => get<WhereverState>(client.stateStore);
|
|
51
|
+
|
|
52
|
+
beforeEach(() => {
|
|
53
|
+
vi.useFakeTimers();
|
|
54
|
+
installLocalStorage();
|
|
55
|
+
(globalThis as any).localStorage.clear();
|
|
56
|
+
ws = makeWSFactory();
|
|
57
|
+
client = new WhereverClient({
|
|
58
|
+
host: 'localhost',
|
|
59
|
+
port: 1234,
|
|
60
|
+
secure: false,
|
|
61
|
+
WebSocketCtor: ws.ctor,
|
|
62
|
+
});
|
|
63
|
+
client.connect();
|
|
64
|
+
ws.last().open();
|
|
65
|
+
createAndLoad(ws, client);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
afterEach(() => {
|
|
69
|
+
client.disconnect(true);
|
|
70
|
+
vi.useRealTimers();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('marks a sent message as unconfirmed (sending), not delivered', () => {
|
|
74
|
+
const ok = client.sendMessage('hello there');
|
|
75
|
+
expect(ok).toBe(true);
|
|
76
|
+
const m = state().messages.find((x) => x.content === 'hello there')!;
|
|
77
|
+
expect(m.delivery).toBe('sending');
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('confirms the message when the server echoes it back', () => {
|
|
81
|
+
client.sendMessage('confirm me');
|
|
82
|
+
ws.last().receive({
|
|
83
|
+
type: 'message_end',
|
|
84
|
+
sessionId: 'sid-1',
|
|
85
|
+
role: 'user',
|
|
86
|
+
content: 'confirm me',
|
|
87
|
+
});
|
|
88
|
+
const m = state().messages.find((x) => x.content === 'confirm me')!;
|
|
89
|
+
expect(m.delivery).toBeUndefined();
|
|
90
|
+
// No duplicate echo added.
|
|
91
|
+
expect(
|
|
92
|
+
state().messages.filter((x) => x.content === 'confirm me').length,
|
|
93
|
+
).toBe(1);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('flips to failed when no echo arrives within the window (half-open loss)', () => {
|
|
97
|
+
// Half-open: send() reports success but the server never processes it, so
|
|
98
|
+
// no echo is ever received.
|
|
99
|
+
client.sendMessage('lost message');
|
|
100
|
+
expect(
|
|
101
|
+
state().messages.find((x) => x.content === 'lost message')!.delivery,
|
|
102
|
+
).toBe('sending');
|
|
103
|
+
|
|
104
|
+
vi.advanceTimersByTime(12_000);
|
|
105
|
+
const m = state().messages.find((x) => x.content === 'lost message')!;
|
|
106
|
+
expect(m.delivery).toBe('failed');
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('persists an unconfirmed message so a reload can recover it', () => {
|
|
110
|
+
client.sendMessage('recover me on reload');
|
|
111
|
+
// Simulate a reload: a fresh client instance against the same localStorage.
|
|
112
|
+
const ws2 = makeWSFactory();
|
|
113
|
+
const client2 = new WhereverClient({
|
|
114
|
+
host: 'localhost',
|
|
115
|
+
port: 1234,
|
|
116
|
+
secure: false,
|
|
117
|
+
WebSocketCtor: ws2.ctor,
|
|
118
|
+
});
|
|
119
|
+
client2.connect();
|
|
120
|
+
ws2.last().open();
|
|
121
|
+
// History comes back WITHOUT the message (the server never got it).
|
|
122
|
+
createAndLoad(ws2, client2, []);
|
|
123
|
+
|
|
124
|
+
const recovered = get<WhereverState>(client2.stateStore).messages.find(
|
|
125
|
+
(x) => x.content === 'recover me on reload',
|
|
126
|
+
);
|
|
127
|
+
expect(recovered).toBeTruthy();
|
|
128
|
+
expect(recovered!.delivery).toBe('failed');
|
|
129
|
+
client2.disconnect(true);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('does NOT re-surface a message that history confirms was delivered', () => {
|
|
133
|
+
client.sendMessage('this one landed');
|
|
134
|
+
// Reload; history DOES contain it (it was delivered before the drop).
|
|
135
|
+
const ws2 = makeWSFactory();
|
|
136
|
+
const client2 = new WhereverClient({
|
|
137
|
+
host: 'localhost',
|
|
138
|
+
port: 1234,
|
|
139
|
+
secure: false,
|
|
140
|
+
WebSocketCtor: ws2.ctor,
|
|
141
|
+
});
|
|
142
|
+
client2.connect();
|
|
143
|
+
ws2.last().open();
|
|
144
|
+
createAndLoad(ws2, client2, [
|
|
145
|
+
{role: 'user', content: 'this one landed', timestamp: 1_000},
|
|
146
|
+
]);
|
|
147
|
+
|
|
148
|
+
const msgs = get<WhereverState>(client2.stateStore).messages.filter(
|
|
149
|
+
(x) => x.content === 'this one landed',
|
|
150
|
+
);
|
|
151
|
+
// Exactly one, and delivered (no failed duplicate).
|
|
152
|
+
expect(msgs.length).toBe(1);
|
|
153
|
+
expect(msgs[0].delivery).toBeUndefined();
|
|
154
|
+
client2.disconnect(true);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it('resendMessage re-sends a failed message and re-arms confirmation', () => {
|
|
158
|
+
client.sendMessage('retry me');
|
|
159
|
+
vi.advanceTimersByTime(12_000);
|
|
160
|
+
const failed = state().messages.find((x) => x.content === 'retry me')!;
|
|
161
|
+
expect(failed.delivery).toBe('failed');
|
|
162
|
+
|
|
163
|
+
const sock = ws.last();
|
|
164
|
+
const before = sock.sent.filter((f) => f.type === 'message').length;
|
|
165
|
+
const ok = client.resendMessage(failed.id);
|
|
166
|
+
expect(ok).toBe(true);
|
|
167
|
+
expect(sock.sent.filter((f) => f.type === 'message').length).toBe(before + 1);
|
|
168
|
+
expect(
|
|
169
|
+
state().messages.find((x) => x.id === failed.id)!.delivery,
|
|
170
|
+
).toBe('sending');
|
|
171
|
+
|
|
172
|
+
// The echo now confirms it.
|
|
173
|
+
sock.receive({
|
|
174
|
+
type: 'message_end',
|
|
175
|
+
sessionId: 'sid-1',
|
|
176
|
+
role: 'user',
|
|
177
|
+
content: 'retry me',
|
|
178
|
+
});
|
|
179
|
+
expect(
|
|
180
|
+
state().messages.find((x) => x.id === failed.id)!.delivery,
|
|
181
|
+
).toBeUndefined();
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it('discardMessage removes a failed message and clears it from persistence', () => {
|
|
185
|
+
client.sendMessage('drop me');
|
|
186
|
+
vi.advanceTimersByTime(12_000);
|
|
187
|
+
const failed = state().messages.find((x) => x.content === 'drop me')!;
|
|
188
|
+
client.discardMessage(failed.id);
|
|
189
|
+
expect(state().messages.find((x) => x.content === 'drop me')).toBeUndefined();
|
|
190
|
+
expect(
|
|
191
|
+
(globalThis as any).localStorage.getItem('wherever-pending:sid-1'),
|
|
192
|
+
).toBeNull();
|
|
193
|
+
});
|
|
194
|
+
});
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import {describe, it, expect, beforeEach, afterEach, vi} from 'vitest';
|
|
2
|
+
import {get} from 'sveltore';
|
|
3
|
+
import {WhereverClient} from '../src/client.js';
|
|
4
|
+
import {makeWSFactory} from './harness.js';
|
|
5
|
+
|
|
6
|
+
// A message submitted WHILE the agent is streaming must be sent to the relay
|
|
7
|
+
// IMMEDIATELY (the server turns a mid-stream `message` into a steer, injected at
|
|
8
|
+
// the next tool/step boundary). This is the pi-CLI default (Enter -> steer). It
|
|
9
|
+
// must NOT be held locally waiting for the turn to finish -- the old behaviour
|
|
10
|
+
// that also mis-fired mid-turn on the flaky isStreaming debounce (docs/adr/0003).
|
|
11
|
+
|
|
12
|
+
function createAndLoad(
|
|
13
|
+
ws: ReturnType<typeof makeWSFactory>,
|
|
14
|
+
client: WhereverClient,
|
|
15
|
+
{streaming = false}: {streaming?: boolean} = {},
|
|
16
|
+
) {
|
|
17
|
+
client.joinSession('/tmp/project/session.jsonl');
|
|
18
|
+
ws.last().receive({
|
|
19
|
+
type: 'session_created',
|
|
20
|
+
sessionId: 'sid-1',
|
|
21
|
+
sessionFile: '/tmp/project/session.jsonl',
|
|
22
|
+
cwd: '/tmp/project',
|
|
23
|
+
model: 'fake:model',
|
|
24
|
+
isStreaming: streaming,
|
|
25
|
+
});
|
|
26
|
+
ws.last().receive({
|
|
27
|
+
type: 'message_history',
|
|
28
|
+
sessionId: 'sid-1',
|
|
29
|
+
messages: [{role: 'user', content: 'go build it', timestamp: 1}],
|
|
30
|
+
totalCount: 1,
|
|
31
|
+
offset: 0,
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
describe('mid-stream submit steers immediately', () => {
|
|
36
|
+
let ws: ReturnType<typeof makeWSFactory>;
|
|
37
|
+
let client: WhereverClient;
|
|
38
|
+
|
|
39
|
+
beforeEach(() => {
|
|
40
|
+
vi.useFakeTimers();
|
|
41
|
+
ws = makeWSFactory();
|
|
42
|
+
client = new WhereverClient({
|
|
43
|
+
host: 'localhost',
|
|
44
|
+
port: 1234,
|
|
45
|
+
secure: false,
|
|
46
|
+
WebSocketCtor: ws.ctor,
|
|
47
|
+
});
|
|
48
|
+
client.connect();
|
|
49
|
+
ws.last().open();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
afterEach(() => {
|
|
53
|
+
client.disconnect(true);
|
|
54
|
+
vi.useRealTimers();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('sends the message frame NOW while the agent is mid-turn', () => {
|
|
58
|
+
createAndLoad(ws, client, {streaming: true});
|
|
59
|
+
const sock = ws.last();
|
|
60
|
+
expect(get(client.stateStore).isStreaming).toBe(true);
|
|
61
|
+
|
|
62
|
+
// User submits mid-stream. sendMessage must hand a `message` frame to the
|
|
63
|
+
// OPEN socket right away, not park it.
|
|
64
|
+
const ok = client.sendMessage('actually, do X instead');
|
|
65
|
+
expect(ok).toBe(true);
|
|
66
|
+
|
|
67
|
+
const frame = sock.lastSentOfType('message');
|
|
68
|
+
expect(frame).toBeTruthy();
|
|
69
|
+
expect(frame.message).toBe('actually, do X instead');
|
|
70
|
+
|
|
71
|
+
// The optimistic user echo is committed immediately (not withheld pending
|
|
72
|
+
// a later drain), and the turn is still in flight.
|
|
73
|
+
const s = get(client.stateStore);
|
|
74
|
+
expect(s.messages.some((m) => m.role === 'user' && m.content === 'actually, do X instead')).toBe(true);
|
|
75
|
+
expect(s.isStreaming).toBe(true);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('returns false and keeps no phantom echo when disconnected mid-stream', () => {
|
|
79
|
+
createAndLoad(ws, client, {streaming: true});
|
|
80
|
+
const sock = ws.last();
|
|
81
|
+
// Socket drops just as the user submits.
|
|
82
|
+
sock.close();
|
|
83
|
+
|
|
84
|
+
const before = get(client.stateStore).messages.length;
|
|
85
|
+
const ok = client.sendMessage('do X instead');
|
|
86
|
+
expect(ok).toBe(false);
|
|
87
|
+
// No optimistic echo for a send that never left; caller keeps the text.
|
|
88
|
+
expect(get(client.stateStore).messages.length).toBe(before);
|
|
89
|
+
expect(get(client.stateStore).sessionError).toBeTruthy();
|
|
90
|
+
});
|
|
91
|
+
});
|
|
@@ -0,0 +1,93 @@
|
|
|
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 reconstructed from LOADED history must carry the same duration as
|
|
8
|
+
// a live-streamed one, so "Took N.Ns" survives a reload. The server sends the
|
|
9
|
+
// tool_call and tool_result as separate HistoryMessages, each with its own
|
|
10
|
+
// timestamp; the client pairs them and computes startedAt/endedAt.
|
|
11
|
+
|
|
12
|
+
describe('tool duration from loaded history', () => {
|
|
13
|
+
let ws: ReturnType<typeof makeWSFactory>;
|
|
14
|
+
let client: WhereverClient;
|
|
15
|
+
const state = () => get<WhereverState>(client.stateStore);
|
|
16
|
+
|
|
17
|
+
beforeEach(() => {
|
|
18
|
+
vi.useFakeTimers();
|
|
19
|
+
ws = makeWSFactory();
|
|
20
|
+
client = new WhereverClient({
|
|
21
|
+
host: 'localhost',
|
|
22
|
+
port: 1234,
|
|
23
|
+
secure: false,
|
|
24
|
+
WebSocketCtor: ws.ctor,
|
|
25
|
+
});
|
|
26
|
+
client.connect();
|
|
27
|
+
ws.last().open();
|
|
28
|
+
client.joinSession('/tmp/project/session.jsonl');
|
|
29
|
+
ws.last().receive({
|
|
30
|
+
type: 'session_created',
|
|
31
|
+
sessionId: 'sid-1',
|
|
32
|
+
sessionFile: '/tmp/project/session.jsonl',
|
|
33
|
+
cwd: '/tmp/project',
|
|
34
|
+
model: 'fake:model',
|
|
35
|
+
isStreaming: false,
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
afterEach(() => {
|
|
40
|
+
client.disconnect(true);
|
|
41
|
+
vi.useRealTimers();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('pairs a tool_call + tool_result into a duration', () => {
|
|
45
|
+
ws.last().receive({
|
|
46
|
+
type: 'message_history',
|
|
47
|
+
sessionId: 'sid-1',
|
|
48
|
+
messages: [
|
|
49
|
+
{role: 'user', content: 'go', timestamp: 1_000},
|
|
50
|
+
{
|
|
51
|
+
role: 'tool_call',
|
|
52
|
+
content: '{"command":"ls"}',
|
|
53
|
+
timestamp: 10_000,
|
|
54
|
+
toolName: 'bash',
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
role: 'tool_result',
|
|
58
|
+
content: 'a\nb',
|
|
59
|
+
timestamp: 13_400,
|
|
60
|
+
toolName: 'bash',
|
|
61
|
+
isError: false,
|
|
62
|
+
},
|
|
63
|
+
],
|
|
64
|
+
totalCount: 3,
|
|
65
|
+
offset: 0,
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const tool = state().messages.find((m) => m.role === 'tool')!;
|
|
69
|
+
expect(tool.startedAt).toBe(10_000);
|
|
70
|
+
expect(tool.endedAt).toBe(13_400);
|
|
71
|
+
expect(((tool.endedAt! - tool.startedAt!) / 1000).toFixed(1)).toBe('3.4');
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('leaves duration unset for an unmatched (result-less) tool_call', () => {
|
|
75
|
+
ws.last().receive({
|
|
76
|
+
type: 'message_history',
|
|
77
|
+
sessionId: 'sid-1',
|
|
78
|
+
messages: [
|
|
79
|
+
{
|
|
80
|
+
role: 'tool_call',
|
|
81
|
+
content: '{}',
|
|
82
|
+
timestamp: 10_000,
|
|
83
|
+
toolName: 'bash',
|
|
84
|
+
},
|
|
85
|
+
],
|
|
86
|
+
totalCount: 1,
|
|
87
|
+
offset: 0,
|
|
88
|
+
});
|
|
89
|
+
const tool = state().messages.find((m) => m.role === 'tool')!;
|
|
90
|
+
expect(tool.startedAt).toBe(10_000);
|
|
91
|
+
expect(tool.endedAt).toBeUndefined();
|
|
92
|
+
});
|
|
93
|
+
});
|
|
@@ -0,0 +1,86 @@
|
|
|
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
|
+
|
|
6
|
+
// The tool-call timing the UI needs to show "Elapsed N.Ns" (running) and
|
|
7
|
+
// "Took N.Ns" (done), like the pi CLI: tool_start stamps startedAt, tool_end
|
|
8
|
+
// stamps endedAt, and a turn that ends without a tool_end freezes the running
|
|
9
|
+
// tool's endedAt so its duration stops ticking.
|
|
10
|
+
|
|
11
|
+
function makeClient() {
|
|
12
|
+
const c = new WhereverClient({host: 'x', port: 1, secure: false});
|
|
13
|
+
const feed = (msg: unknown) =>
|
|
14
|
+
(c as unknown as {handleMessage(m: unknown): void}).handleMessage(msg);
|
|
15
|
+
const state = () => get<WhereverState>(c.stateStore);
|
|
16
|
+
const lastTool = () =>
|
|
17
|
+
[...state().messages].reverse().find((m) => m.role === 'tool');
|
|
18
|
+
return {feed, state, lastTool};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const SESSION = 'sess-1';
|
|
22
|
+
|
|
23
|
+
beforeEach(() => vi.useFakeTimers());
|
|
24
|
+
afterEach(() => {
|
|
25
|
+
vi.clearAllTimers();
|
|
26
|
+
vi.useRealTimers();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe('tool call timing', () => {
|
|
30
|
+
it('stamps startedAt on tool_start and endedAt on tool_end', () => {
|
|
31
|
+
const {feed, lastTool} = makeClient();
|
|
32
|
+
feed({type: 'agent_start', sessionId: SESSION});
|
|
33
|
+
|
|
34
|
+
vi.setSystemTime(1_000);
|
|
35
|
+
feed({
|
|
36
|
+
type: 'tool_start',
|
|
37
|
+
sessionId: SESSION,
|
|
38
|
+
toolName: 'bash',
|
|
39
|
+
args: {command: 'ls'},
|
|
40
|
+
});
|
|
41
|
+
const running = lastTool()!;
|
|
42
|
+
expect(running.startedAt).toBe(1_000);
|
|
43
|
+
expect(running.endedAt).toBeUndefined();
|
|
44
|
+
expect(running.isStreaming).toBe(true);
|
|
45
|
+
|
|
46
|
+
vi.setSystemTime(3_500);
|
|
47
|
+
feed({type: 'tool_end', sessionId: SESSION, toolName: 'bash', result: 'a\nb'});
|
|
48
|
+
const done = lastTool()!;
|
|
49
|
+
expect(done.startedAt).toBe(1_000);
|
|
50
|
+
expect(done.endedAt).toBe(3_500);
|
|
51
|
+
expect(done.isStreaming).toBe(false);
|
|
52
|
+
// 2.5s took.
|
|
53
|
+
expect(((done.endedAt! - done.startedAt!) / 1000).toFixed(1)).toBe('2.5');
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('freezes a running tool endedAt when the turn ends without tool_end', () => {
|
|
57
|
+
const {feed, lastTool} = makeClient();
|
|
58
|
+
feed({type: 'agent_start', sessionId: SESSION});
|
|
59
|
+
vi.setSystemTime(1_000);
|
|
60
|
+
feed({type: 'tool_start', sessionId: SESSION, toolName: 'bash', args: {}});
|
|
61
|
+
expect(lastTool()!.endedAt).toBeUndefined();
|
|
62
|
+
|
|
63
|
+
// Turn ends between steps with no tool_end for the running tool.
|
|
64
|
+
vi.setSystemTime(2_000);
|
|
65
|
+
feed({type: 'agent_end', sessionId: SESSION});
|
|
66
|
+
// The 300ms agent_end debounce fires; fake time advances to 2_300 and the
|
|
67
|
+
// callback stamps endedAt = Date.now() at that point.
|
|
68
|
+
vi.advanceTimersByTime(300);
|
|
69
|
+
|
|
70
|
+
const frozen = lastTool()!;
|
|
71
|
+
expect(frozen.isStreaming).toBe(false);
|
|
72
|
+
expect(frozen.endedAt).toBe(2_300);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('freezes a running tool endedAt on abort', () => {
|
|
76
|
+
const {feed, lastTool} = makeClient();
|
|
77
|
+
feed({type: 'agent_start', sessionId: SESSION});
|
|
78
|
+
vi.setSystemTime(1_000);
|
|
79
|
+
feed({type: 'tool_start', sessionId: SESSION, toolName: 'bash', args: {}});
|
|
80
|
+
vi.setSystemTime(5_000);
|
|
81
|
+
feed({type: 'aborted', sessionId: SESSION});
|
|
82
|
+
const frozen = lastTool()!;
|
|
83
|
+
expect(frozen.isStreaming).toBe(false);
|
|
84
|
+
expect(frozen.endedAt).toBe(5_000);
|
|
85
|
+
});
|
|
86
|
+
});
|