@wherever-dev/client 0.8.0 → 0.8.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.
Files changed (38) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +63 -0
  3. package/dist/client.js +2 -0
  4. package/dist/client.js.map +1 -1
  5. package/dist/types.d.ts +1 -0
  6. package/dist/types.js.map +1 -1
  7. package/package.json +23 -3
  8. package/src/client.ts +2 -0
  9. package/src/types.ts +3 -0
  10. package/test/agent-pending.test.ts +0 -107
  11. package/test/chunked-upload.test.ts +0 -483
  12. package/test/conversation-mode-flag.test.ts +0 -111
  13. package/test/creating-session-watchdog.test.ts +0 -82
  14. package/test/folder-conflict-read-only.test.ts +0 -145
  15. package/test/folder-missing-read-only.test.ts +0 -114
  16. package/test/harness.ts +0 -133
  17. package/test/hydration-seam.test.ts +0 -400
  18. package/test/liveness-probe.test.ts +0 -183
  19. package/test/message-delivery-confirmation.test.ts +0 -320
  20. package/test/mid-stream-steer.test.ts +0 -91
  21. package/test/new-session-clone-progress.test.ts +0 -263
  22. package/test/optimistic-force-command.test.ts +0 -127
  23. package/test/queued-steer-restored.test.ts +0 -175
  24. package/test/reconnect-rejoins-session.test.ts +0 -136
  25. package/test/restore-panel.test.ts +0 -324
  26. package/test/resume-keeps-session.test.ts +0 -118
  27. package/test/session-load-by-id.test.ts +0 -100
  28. package/test/session-reload.test.ts +0 -82
  29. package/test/skills-autocomplete.test.ts +0 -113
  30. package/test/steer-queue-cancel.test.ts +0 -130
  31. package/test/superseded-connection-key.test.ts +0 -70
  32. package/test/superseded-session-load.test.ts +0 -180
  33. package/test/tool-aborted-render.test.ts +0 -170
  34. package/test/tool-duration-history.test.ts +0 -93
  35. package/test/tool-duration.test.ts +0 -86
  36. package/test/tool-interrupted-history.test.ts +0 -294
  37. package/test/upload-survives-reconnect.test.ts +0 -482
  38. package/tsconfig.json +0 -16
@@ -1,113 +0,0 @@
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
- // `/skill:` composer autocomplete contract. The skill list is only resolvable
7
- // once the session's live agent exists, so the client asks for it as soon as
8
- // the agent is live. A brand-new session (session_new) is live IMMEDIATELY and
9
- // never emits session_ready, so requesting only on session_ready left a fresh
10
- // session with an empty skill menu (the regression this covers).
11
-
12
- describe('skill autocomplete list', () => {
13
- let ws: ReturnType<typeof makeWSFactory>;
14
- let client: WhereverClient;
15
-
16
- beforeEach(() => {
17
- vi.useFakeTimers();
18
- ws = makeWSFactory();
19
- client = new WhereverClient({
20
- host: 'localhost',
21
- port: 1234,
22
- secure: false,
23
- WebSocketCtor: ws.ctor,
24
- });
25
- client.connect();
26
- ws.last().open();
27
- });
28
-
29
- afterEach(() => {
30
- client.disconnect(true);
31
- vi.useRealTimers();
32
- });
33
-
34
- it('requests skills for a freshly created session (no session_ready)', () => {
35
- client.createSession('/tmp/new');
36
- ws.last().receive({
37
- type: 'session_created',
38
- sessionId: 'sid-new',
39
- sessionFile: '/tmp/new/session.jsonl',
40
- cwd: '/tmp/new',
41
- model: 'fake:model',
42
- isStreaming: false,
43
- // no pending flag: the agent is already live
44
- });
45
-
46
- const req = ws.last().lastSentOfType('skills_request');
47
- expect(req).toBeDefined();
48
- expect(req.sessionId).toBe('sid-new');
49
-
50
- ws.last().receive({
51
- type: 'skills_list',
52
- sessionId: 'sid-new',
53
- skills: [{name: 'skill:setup', description: 'Set things up'}],
54
- });
55
- expect(get(client.stateStore).skills).toEqual([
56
- {name: 'skill:setup', description: 'Set things up'},
57
- ]);
58
- });
59
-
60
- it('defers the request to session_ready for a cold (pending) load', () => {
61
- client.joinSession('/tmp/p/session.jsonl');
62
- ws.last().receive({
63
- type: 'session_created',
64
- sessionId: 'sid-cold',
65
- sessionFile: '/tmp/p/session.jsonl',
66
- cwd: '/tmp/p',
67
- model: '',
68
- isStreaming: false,
69
- pending: true,
70
- });
71
- // The agent is still building: nothing to ask yet.
72
- expect(ws.last().lastSentOfType('skills_request')).toBeUndefined();
73
-
74
- ws.last().receive({
75
- type: 'session_ready',
76
- sessionId: 'sid-cold',
77
- sessionFile: '/tmp/p/session.jsonl',
78
- model: 'fake:model',
79
- isStreaming: false,
80
- });
81
- expect(ws.last().lastSentOfType('skills_request')?.sessionId).toBe('sid-cold');
82
- });
83
-
84
- it('clears the previous session skills when attaching to another session', () => {
85
- client.createSession('/tmp/a');
86
- ws.last().receive({
87
- type: 'session_created',
88
- sessionId: 'sid-a',
89
- sessionFile: '/tmp/a/session.jsonl',
90
- cwd: '/tmp/a',
91
- model: 'fake:model',
92
- isStreaming: false,
93
- });
94
- ws.last().receive({
95
- type: 'skills_list',
96
- sessionId: 'sid-a',
97
- skills: [{name: 'skill:a', description: 'A'}],
98
- });
99
- expect(get(client.stateStore).skills.length).toBe(1);
100
-
101
- client.joinSession('/tmp/b/session.jsonl');
102
- ws.last().receive({
103
- type: 'session_created',
104
- sessionId: 'sid-b',
105
- sessionFile: '/tmp/b/session.jsonl',
106
- cwd: '/tmp/b',
107
- model: 'fake:model',
108
- isStreaming: false,
109
- pending: true,
110
- });
111
- expect(get(client.stateStore).skills).toEqual([]);
112
- });
113
- });
@@ -1,130 +0,0 @@
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 mid-stream steer is queued on the server (pi injects it at the next step
8
- // boundary). The server relays the current queue via `queue_update`, and the
9
- // client tracks it as `pendingSteering` so the UI can offer a per-message
10
- // "Cancel". Cancelling sends `cancel_steer` (which clears the server queue via
11
- // pi's clearQueue) WITHOUT aborting the running turn.
12
-
13
- function createAndLoad(
14
- ws: ReturnType<typeof makeWSFactory>,
15
- client: WhereverClient,
16
- {streaming = true}: {streaming?: boolean} = {},
17
- ) {
18
- client.joinSession('/tmp/project/session.jsonl');
19
- ws.last().receive({
20
- type: 'session_created',
21
- sessionId: 'sid-1',
22
- sessionFile: '/tmp/project/session.jsonl',
23
- cwd: '/tmp/project',
24
- model: 'fake:model',
25
- isStreaming: streaming,
26
- });
27
- ws.last().receive({
28
- type: 'message_history',
29
- sessionId: 'sid-1',
30
- messages: [{role: 'user', content: 'go build it', timestamp: 1}],
31
- totalCount: 1,
32
- offset: 0,
33
- });
34
- }
35
-
36
- describe('queued steer tracking + cancel', () => {
37
- let ws: ReturnType<typeof makeWSFactory>;
38
- let client: WhereverClient;
39
- const state = () => get<WhereverState>(client.stateStore);
40
-
41
- beforeEach(() => {
42
- vi.useFakeTimers();
43
- ws = makeWSFactory();
44
- client = new WhereverClient({
45
- host: 'localhost',
46
- port: 1234,
47
- secure: false,
48
- WebSocketCtor: ws.ctor,
49
- });
50
- client.connect();
51
- ws.last().open();
52
- createAndLoad(ws, client);
53
- });
54
-
55
- afterEach(() => {
56
- client.disconnect(true);
57
- vi.useRealTimers();
58
- });
59
-
60
- it('tracks the queued steer from queue_update', () => {
61
- expect(state().pendingSteering).toEqual([]);
62
- ws.last().receive({
63
- type: 'queue_update',
64
- sessionId: 'sid-1',
65
- steering: ['do X instead'],
66
- });
67
- expect(state().pendingSteering).toEqual(['do X instead']);
68
- });
69
-
70
- it('replaces the pending set outright on each queue_update', () => {
71
- ws.last().receive({
72
- type: 'queue_update',
73
- sessionId: 'sid-1',
74
- steering: ['first steer', 'second steer'],
75
- });
76
- expect(state().pendingSteering).toEqual(['first steer', 'second steer']);
77
- // A later update with the first delivered leaves only the second.
78
- ws.last().receive({
79
- type: 'queue_update',
80
- sessionId: 'sid-1',
81
- steering: ['second steer'],
82
- });
83
- expect(state().pendingSteering).toEqual(['second steer']);
84
- // Emptied when the queue drains.
85
- ws.last().receive({type: 'queue_update', sessionId: 'sid-1', steering: []});
86
- expect(state().pendingSteering).toEqual([]);
87
- });
88
-
89
- it('cancelSteer sends cancel_steer and optimistically clears the pending set', () => {
90
- ws.last().receive({
91
- type: 'queue_update',
92
- sessionId: 'sid-1',
93
- steering: ['retract me'],
94
- });
95
- expect(state().pendingSteering).toEqual(['retract me']);
96
-
97
- const sock = ws.last();
98
- client.cancelSteer();
99
-
100
- const frame = sock.lastSentOfType('cancel_steer');
101
- expect(frame).toBeTruthy();
102
- expect(frame.sessionId).toBe('sid-1');
103
- // Optimistically cleared so the Cancel affordance disappears immediately.
104
- expect(state().pendingSteering).toEqual([]);
105
- // It does NOT abort the turn.
106
- expect(sock.lastSentOfType('abort')).toBeUndefined();
107
- });
108
-
109
- it('clears the pending set when the turn ends', () => {
110
- ws.last().receive({
111
- type: 'queue_update',
112
- sessionId: 'sid-1',
113
- steering: ['queued steer'],
114
- });
115
- expect(state().pendingSteering).toEqual(['queued steer']);
116
-
117
- ws.last().receive({type: 'agent_end', sessionId: 'sid-1'});
118
- // agent_end resolves on a short debounce; advance it.
119
- vi.advanceTimersByTime(500);
120
- expect(state().pendingSteering).toEqual([]);
121
- });
122
-
123
- it('cancelSteer with no session is a no-op (no frame sent)', () => {
124
- client.leaveSession();
125
- const sock = ws.last();
126
- const before = sock.sent.length;
127
- client.cancelSteer();
128
- expect(sock.sent.length).toBe(before);
129
- });
130
- });
@@ -1,70 +0,0 @@
1
- import {describe, it, expect, beforeEach, afterEach, vi} from 'vitest';
2
- import {WhereverClient} from '../src/client.js';
3
- import {makeWSFactory} from './harness.js';
4
-
5
- // The clientKey lets the server retire this viewer's own superseded (silently
6
- // dropped) connection instead of counting it as a second viewer. But the web
7
- // client scopes the key per tab via sessionStorage, and "Duplicate tab" CLONES
8
- // sessionStorage: two LIVE tabs can end up sharing one key and evicting each
9
- // other forever, one reconnect apart.
10
- //
11
- // The server therefore tells a connection it is being retired. Receiving that
12
- // proves this client is alive, so it takes a fresh key before the reconnect --
13
- // one eviction, then both tabs are distinct viewers again.
14
-
15
- describe('superseded connection', () => {
16
- let ws: ReturnType<typeof makeWSFactory>;
17
- let client: WhereverClient;
18
- let persisted: string[];
19
-
20
- function keyOf(url: string): string | null {
21
- return new URL(url).searchParams.get('clientKey');
22
- }
23
-
24
- beforeEach(() => {
25
- vi.useFakeTimers();
26
- ws = makeWSFactory();
27
- persisted = [];
28
- client = new WhereverClient({
29
- host: 'localhost',
30
- port: 1234,
31
- secure: false,
32
- WebSocketCtor: ws.ctor,
33
- clientKey: 'shared-key',
34
- onClientKeyChange: (k) => persisted.push(k),
35
- });
36
- client.connect();
37
- ws.last().open();
38
- });
39
-
40
- afterEach(() => {
41
- client.disconnect(true);
42
- vi.useRealTimers();
43
- });
44
-
45
- it('sends the configured key on connect', () => {
46
- expect(keyOf(ws.last().url)).toBe('shared-key');
47
- });
48
-
49
- it('takes (and reports) a new key when the server says it was superseded', () => {
50
- ws.last().receive({type: 'connection_superseded'});
51
-
52
- expect(persisted).toHaveLength(1);
53
- expect(persisted[0]).not.toBe('shared-key');
54
-
55
- // The server closes right after; the reconnect must carry the NEW key, so
56
- // the two tabs stop evicting each other.
57
- ws.last().close();
58
- vi.advanceTimersByTime(5000);
59
- const reconnected = ws.last();
60
- expect(keyOf(reconnected.url)).toBe(persisted[0]);
61
- });
62
-
63
- it('keeps its key across an ordinary reconnect', () => {
64
- ws.last().close();
65
- vi.advanceTimersByTime(5000);
66
-
67
- expect(keyOf(ws.last().url)).toBe('shared-key');
68
- expect(persisted).toHaveLength(0);
69
- });
70
- });
@@ -1,180 +0,0 @@
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
- // Bug: while a session was slow to load, tapping a DIFFERENT session in the
7
- // sidebar could result in the old, slow session suddenly replacing the one the
8
- // user had switched to. The user is looking at session B, then session A's late
9
- // reply lands and clobbers B.
10
- //
11
- // Cause: session_load's reply (session_created + message_history) was applied
12
- // UNCONDITIONALLY. A switch to B reset the store and issued a fresh load, but
13
- // A's in-flight replies were still on the wire; when they arrived they set
14
- // activeSessionFile/sessionId/messages back to A.
15
- //
16
- // Fix: stamp pendingLoadFile on every session_load and reject a session_created
17
- // whose sessionFile no longer matches (the latest tap wins). message_history for
18
- // a non-active sessionId is likewise dropped, and it must not disarm the
19
- // watchdog still guarding B's in-flight load.
20
-
21
- function newClient(ws: ReturnType<typeof makeWSFactory>) {
22
- const client = new WhereverClient({
23
- host: 'localhost',
24
- port: 1234,
25
- secure: false,
26
- WebSocketCtor: ws.ctor,
27
- });
28
- client.connect();
29
- ws.last().open();
30
- return client;
31
- }
32
-
33
- describe('a superseded (slow) session load never clobbers the switched-to session', () => {
34
- let ws: ReturnType<typeof makeWSFactory>;
35
- let client: WhereverClient;
36
-
37
- beforeEach(() => {
38
- vi.useFakeTimers();
39
- ws = makeWSFactory();
40
- client = newClient(ws);
41
- });
42
-
43
- afterEach(() => {
44
- client.disconnect(true);
45
- vi.useRealTimers();
46
- });
47
-
48
- it("drops session A's late session_created after the user switched to B", () => {
49
- // Start loading the slow session A.
50
- client.joinSession('/tmp/project/A.jsonl');
51
- expect(get(client.stateStore).loadingSession).toBe(true);
52
-
53
- // Before A replies, the user taps session B in the sidebar.
54
- client.switchSession('/tmp/project/B.jsonl');
55
-
56
- // B loads normally.
57
- ws.last().receive({
58
- type: 'session_created',
59
- sessionId: 'sid-B',
60
- sessionFile: '/tmp/project/B.jsonl',
61
- cwd: '/tmp/project',
62
- model: 'fake:model',
63
- });
64
- ws.last().receive({
65
- type: 'message_history',
66
- sessionId: 'sid-B',
67
- messages: [{role: 'user', content: 'work on B', timestamp: 1}],
68
- totalCount: 1,
69
- offset: 0,
70
- });
71
-
72
- expect(get(client.stateStore).activeSessionFile).toBe('/tmp/project/B.jsonl');
73
- expect(get(client.stateStore).sessionId).toBe('sid-B');
74
-
75
- // NOW session A's slow reply finally arrives. It must be ignored: the user
76
- // is on B, and A should not suddenly take over.
77
- ws.last().receive({
78
- type: 'session_created',
79
- sessionId: 'sid-A',
80
- sessionFile: '/tmp/project/A.jsonl',
81
- cwd: '/tmp/project',
82
- model: 'fake:model',
83
- });
84
- ws.last().receive({
85
- type: 'message_history',
86
- sessionId: 'sid-A',
87
- messages: [{role: 'user', content: 'work on A', timestamp: 1}],
88
- totalCount: 1,
89
- offset: 0,
90
- });
91
-
92
- const s = get(client.stateStore);
93
- expect(s.activeSessionFile).toBe('/tmp/project/B.jsonl');
94
- expect(s.sessionId).toBe('sid-B');
95
- expect(s.messages.map((m) => m.content)).toContain('work on B');
96
- expect(s.messages.map((m) => m.content)).not.toContain('work on A');
97
- });
98
-
99
- it('accepts the switched-to session B normally (the happy path is unaffected)', () => {
100
- client.joinSession('/tmp/project/A.jsonl');
101
- client.switchSession('/tmp/project/B.jsonl');
102
-
103
- ws.last().receive({
104
- type: 'session_created',
105
- sessionId: 'sid-B',
106
- sessionFile: '/tmp/project/B.jsonl',
107
- cwd: '/tmp/project',
108
- model: 'fake:model',
109
- });
110
- ws.last().receive({
111
- type: 'message_history',
112
- sessionId: 'sid-B',
113
- messages: [{role: 'user', content: 'work on B', timestamp: 1}],
114
- totalCount: 1,
115
- offset: 0,
116
- });
117
-
118
- const s = get(client.stateStore);
119
- expect(s.activeSessionFile).toBe('/tmp/project/B.jsonl');
120
- expect(s.loadingSession).toBe(false);
121
- });
122
-
123
- it("a stale session A history does not disarm the watchdog guarding B's load", () => {
124
- client.joinSession('/tmp/project/A.jsonl');
125
- client.switchSession('/tmp/project/B.jsonl');
126
-
127
- // B is accepted...
128
- ws.last().receive({
129
- type: 'session_created',
130
- sessionId: 'sid-B',
131
- sessionFile: '/tmp/project/B.jsonl',
132
- cwd: '/tmp/project',
133
- model: 'fake:model',
134
- });
135
- // ...but B's own message_history has NOT arrived yet, so B is still loading
136
- // and its load watchdog is armed. A stale A history now shows up.
137
- ws.last().receive({
138
- type: 'message_history',
139
- sessionId: 'sid-A',
140
- messages: [{role: 'user', content: 'work on A', timestamp: 1}],
141
- totalCount: 1,
142
- offset: 0,
143
- });
144
-
145
- // The stale A history must NOT have resolved B's load: still loading, and
146
- // the watchdog is still able to fire as a last resort.
147
- expect(get(client.stateStore).loadingSession).toBe(true);
148
-
149
- // B's real history finally lands and resolves the load.
150
- ws.last().receive({
151
- type: 'message_history',
152
- sessionId: 'sid-B',
153
- messages: [{role: 'user', content: 'work on B', timestamp: 1}],
154
- totalCount: 1,
155
- offset: 0,
156
- });
157
- expect(get(client.stateStore).loadingSession).toBe(false);
158
- });
159
-
160
- it('a brand-new session (session_new) is never rejected by a stale pendingLoadFile', () => {
161
- // A slow load is in flight...
162
- client.joinSession('/tmp/project/A.jsonl');
163
- // ...then the user creates a NEW session instead. pendingLoadFile must be
164
- // cleared so the new session's created (with an unrelated sessionFile) is
165
- // accepted.
166
- client.createSession('/tmp/project');
167
- ws.last().receive({
168
- type: 'session_created',
169
- sessionId: 'sid-N',
170
- sessionFile: '/tmp/project/new.jsonl',
171
- cwd: '/tmp/project',
172
- model: 'fake:model',
173
- });
174
-
175
- const s = get(client.stateStore);
176
- expect(s.activeSessionFile).toBe('/tmp/project/new.jsonl');
177
- expect(s.sessionId).toBe('sid-N');
178
- expect(s.creatingSession).toBe(false);
179
- });
180
- });
@@ -1,170 +0,0 @@
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
- // When a running tool is ABORTED (e.g. the web "abort" button kills it mid-run),
8
- // pi surfaces it as an errored result with a trailing "...aborted" status. That
9
- // is NOT a tool failure, so the UI must render a neutral "interrupted" state,
10
- // not a red error. A tool that genuinely completed (or genuinely failed) before
11
- // the abort keeps its real success/error state.
12
-
13
- function setup(ws: ReturnType<typeof makeWSFactory>, isStreaming = true) {
14
- const client = new WhereverClient({
15
- host: 'localhost',
16
- port: 1234,
17
- secure: false,
18
- WebSocketCtor: ws.ctor,
19
- });
20
- client.connect();
21
- ws.last().open();
22
- client.joinSession('/tmp/project/session.jsonl');
23
- ws.last().receive({
24
- type: 'session_created',
25
- sessionId: 'sid-1',
26
- sessionFile: '/tmp/project/session.jsonl',
27
- cwd: '/tmp/project',
28
- model: 'fake:model',
29
- isStreaming,
30
- });
31
- return client;
32
- }
33
-
34
- describe('aborted tool call rendering', () => {
35
- let ws: ReturnType<typeof makeWSFactory>;
36
- let client: WhereverClient;
37
- const state = () => get<WhereverState>(client.stateStore);
38
-
39
- beforeEach(() => {
40
- vi.useFakeTimers();
41
- ws = makeWSFactory();
42
- });
43
-
44
- afterEach(() => {
45
- client.disconnect(true);
46
- vi.useRealTimers();
47
- });
48
-
49
- it('live tool_end: an aborted result is interrupted, not an error', () => {
50
- client = setup(ws);
51
- ws.last().receive({type: 'tool_start', sessionId: 'sid-1', toolName: 'bash', args: {command: 'sleep 8'}});
52
- ws.last().receive({
53
- type: 'tool_end',
54
- sessionId: 'sid-1',
55
- toolName: 'bash',
56
- isError: true,
57
- result: 'Command aborted',
58
- });
59
- const tool = state().messages.find((m) => m.role === 'tool')!;
60
- expect(tool.interrupted).toBe(true);
61
- expect(tool.isError).toBe(false);
62
- });
63
-
64
- it('live tool_end: a genuine error stays an error', () => {
65
- client = setup(ws);
66
- ws.last().receive({type: 'tool_start', sessionId: 'sid-1', toolName: 'bash', args: {command: 'false'}});
67
- ws.last().receive({
68
- type: 'tool_end',
69
- sessionId: 'sid-1',
70
- toolName: 'bash',
71
- isError: true,
72
- result: 'boom\n\nCommand exited with code 1',
73
- });
74
- const tool = state().messages.find((m) => m.role === 'tool')!;
75
- expect(tool.isError).toBe(true);
76
- expect(tool.interrupted).toBeFalsy();
77
- });
78
-
79
- it('live tool_end: a genuine success stays a success', () => {
80
- client = setup(ws);
81
- ws.last().receive({type: 'tool_start', sessionId: 'sid-1', toolName: 'bash', args: {command: 'echo quick'}});
82
- ws.last().receive({
83
- type: 'tool_end',
84
- sessionId: 'sid-1',
85
- toolName: 'bash',
86
- isError: false,
87
- result: 'quick',
88
- });
89
- const tool = state().messages.find((m) => m.role === 'tool')!;
90
- expect(tool.isError).toBe(false);
91
- expect(tool.interrupted).toBeFalsy();
92
- });
93
-
94
- it('history: two parallel tools, one completed (green) and one aborted (interrupted)', () => {
95
- client = setup(ws, false);
96
- ws.last().receive({
97
- type: 'message_history',
98
- sessionId: 'sid-1',
99
- messages: [
100
- {role: 'user', content: 'go', timestamp: 1_000},
101
- {role: 'tool_call', content: '{"command":"echo quick"}', timestamp: 2_000, toolName: 'bash'},
102
- {role: 'tool_call', content: '{"command":"sleep 8"}', timestamp: 2_001, toolName: 'bash'},
103
- {role: 'tool_result', content: 'quick', timestamp: 3_000, toolName: 'bash', isError: false},
104
- {role: 'tool_result', content: 'Command aborted', timestamp: 3_100, toolName: 'bash', isError: true},
105
- ],
106
- totalCount: 5,
107
- offset: 0,
108
- });
109
- const tools = state().messages.filter((m) => m.role === 'tool');
110
- expect(tools.length).toBe(2);
111
- // FIFO pairing by tool name: first result -> first call (echo, success),
112
- // second result -> second call (sleep, aborted -> interrupted).
113
- const succeeded = tools.find((t) => t.toolOutput === 'quick')!;
114
- const aborted = tools.find((t) => (t.toolOutput || '').includes('aborted'))!;
115
- expect(succeeded.isError).toBeFalsy();
116
- expect(succeeded.interrupted).toBeFalsy();
117
- expect(aborted.isError).toBe(false);
118
- expect(aborted.interrupted).toBe(true);
119
- });
120
-
121
- it('two parallel same-named tools both aborted: BOTH interrupted (no stray green)', () => {
122
- client = setup(ws);
123
- // Two parallel bash calls start (same tool name).
124
- ws.last().receive({type: 'tool_start', sessionId: 'sid-1', toolName: 'bash', args: {command: 'sleep 8'}});
125
- ws.last().receive({type: 'tool_start', sessionId: 'sid-1', toolName: 'bash', args: {command: 'sleep 8'}});
126
- // Both settle as aborted. FIFO matching must route each tool_end to a
127
- // DISTINCT streaming message, so neither is left to be finalized green.
128
- ws.last().receive({type: 'tool_end', sessionId: 'sid-1', toolName: 'bash', isError: true, result: 'Command aborted'});
129
- ws.last().receive({type: 'tool_end', sessionId: 'sid-1', toolName: 'bash', isError: true, result: 'Command aborted'});
130
-
131
- const tools = state().messages.filter((m) => m.role === 'tool');
132
- expect(tools.length).toBe(2);
133
- for (const t of tools) {
134
- expect(t.isStreaming).toBe(false);
135
- expect(t.interrupted).toBe(true);
136
- expect(t.isError).toBe(false);
137
- }
138
- });
139
-
140
- it('a tool still streaming at agent_end is interrupted, not a green success', () => {
141
- client = setup(ws);
142
- vi.useFakeTimers();
143
- ws.last().receive({type: 'tool_start', sessionId: 'sid-1', toolName: 'bash', args: {command: 'sleep 8'}});
144
- // The turn ends WITHOUT a tool_end for this tool (e.g. aborted, or the
145
- // result frame was lost). agent_end finalizes after a 300ms debounce.
146
- ws.last().receive({type: 'agent_end', sessionId: 'sid-1'});
147
- vi.advanceTimersByTime(350);
148
-
149
- const tool = state().messages.find((m) => m.role === 'tool')!;
150
- expect(tool.isStreaming).toBe(false);
151
- expect(tool.interrupted).toBe(true);
152
- expect(tool.isError).toBeFalsy();
153
- });
154
-
155
- it('an "aborted" substring in normal output is NOT treated as an abort', () => {
156
- client = setup(ws);
157
- ws.last().receive({type: 'tool_start', sessionId: 'sid-1', toolName: 'bash', args: {command: 'echo'}});
158
- ws.last().receive({
159
- type: 'tool_end',
160
- sessionId: 'sid-1',
161
- toolName: 'bash',
162
- isError: true,
163
- result: 'the build was aborted by the linter\n\nCommand exited with code 2',
164
- });
165
- const tool = state().messages.find((m) => m.role === 'tool')!;
166
- // Trailing status line is "Command exited with code 2", so it is a real error.
167
- expect(tool.isError).toBe(true);
168
- expect(tool.interrupted).toBeFalsy();
169
- });
170
- });