@wherever-dev/client 0.2.2 → 0.3.0
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 +22 -0
- package/dist/client.d.ts +5 -0
- package/dist/client.js +194 -11
- package/dist/client.js.map +1 -1
- package/dist/types.d.ts +1 -0
- package/package.json +6 -3
- package/src/client.ts +206 -13
- package/src/types.ts +7 -0
- package/test/agent-pending.test.ts +107 -0
- package/test/creating-session-watchdog.test.ts +82 -0
- package/test/harness.ts +97 -0
- package/test/reconnect-rejoins-session.test.ts +136 -0
- package/test/resume-keeps-session.test.ts +118 -0
package/src/client.ts
CHANGED
|
@@ -17,6 +17,7 @@ const defaultState: WhereverState = {
|
|
|
17
17
|
creatingSession: false,
|
|
18
18
|
loadingSession: false,
|
|
19
19
|
resyncing: false,
|
|
20
|
+
agentPending: false,
|
|
20
21
|
error: null,
|
|
21
22
|
session: null,
|
|
22
23
|
sessionId: null,
|
|
@@ -53,6 +54,18 @@ export class WhereverClient {
|
|
|
53
54
|
// clear the flags and surface a recoverable error instead of an endless spin.
|
|
54
55
|
private loadWatchdog: any = null;
|
|
55
56
|
private static readonly LOAD_WATCHDOG_MS = 12_000;
|
|
57
|
+
// Watchdog for an in-flight session_new. Symmetrical to loadWatchdog: creating
|
|
58
|
+
// a session sets creatingSession=true and shows a BLOCKING full-screen overlay,
|
|
59
|
+
// and the only things that clear it are session_created/session_error/
|
|
60
|
+
// session_conflict/session_interrupted (or a socket close). If none of those
|
|
61
|
+
// ever arrives (a slow git init / remote-repo creation, an error thrown before
|
|
62
|
+
// the reply is sent, or a half-open socket the liveness watchdog has not yet
|
|
63
|
+
// reaped), the overlay spins forever and the only recovery is a reload. This
|
|
64
|
+
// timer guarantees creatingSession always resolves. It is longer than the load
|
|
65
|
+
// watchdog because creating can legitimately take a while (git init + creating
|
|
66
|
+
// a remote GitHub repo over the network).
|
|
67
|
+
private createWatchdog: any = null;
|
|
68
|
+
private static readonly CREATE_WATCHDOG_MS = 25_000;
|
|
56
69
|
private reconnectAttempts = 0;
|
|
57
70
|
private reconnectDelay = 2000;
|
|
58
71
|
private maxReconnectDelay = 15000;
|
|
@@ -172,14 +185,39 @@ export class WhereverClient {
|
|
|
172
185
|
// resynced in place. resyncing stays true until message_history (or an
|
|
173
186
|
// error/conflict) arrives, keeping the "reconnecting" affordance up and
|
|
174
187
|
// input blocked without dropping the cached messages.
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
188
|
+
//
|
|
189
|
+
// Re-attachment is REQUIRED on every reconnect that still holds a session,
|
|
190
|
+
// not just the suspend/resume path. The server is stateless per socket:
|
|
191
|
+
// each new connection starts attached to NO session (the old socket's
|
|
192
|
+
// close removed this client from the pool). If we do not re-issue
|
|
193
|
+
// session_load here, the relay is connected but the session stream is
|
|
194
|
+
// dead, so the UI shows a frozen conversation (a stale tool call as the
|
|
195
|
+
// last message, Abort disabled) while the agent keeps running headless.
|
|
196
|
+
// Two sources for what to rejoin:
|
|
197
|
+
// 1. resumeSessionFile -> explicit suspend()/resume() (tab background).
|
|
198
|
+
// 2. the store's activeSessionFile -> an UNSOLICITED drop (network
|
|
199
|
+
// blip, tab switch, laptop sleep, half-open reap) where suspend()
|
|
200
|
+
// never ran. This is the case that used to silently freeze.
|
|
201
|
+
const cached = get(this.stateStore);
|
|
202
|
+
const rejoinFile = this.resumeSessionFile ?? cached.activeSessionFile;
|
|
203
|
+
const rejoinCwd =
|
|
204
|
+
this.resumeSessionFile != null
|
|
205
|
+
? this.resumeCwd
|
|
206
|
+
: (cached.activeCwd ?? undefined);
|
|
207
|
+
const rejoinModel =
|
|
208
|
+
this.resumeSessionFile != null
|
|
209
|
+
? this.resumeModel
|
|
210
|
+
: (cached.activeModel ?? undefined);
|
|
211
|
+
this.resumeSessionFile = null;
|
|
212
|
+
this.resumeCwd = undefined;
|
|
213
|
+
this.resumeModel = undefined;
|
|
214
|
+
if (rejoinFile) {
|
|
215
|
+
// Keep the cached conversation on screen but mark it resyncing so the
|
|
216
|
+
// "Reconnecting and syncing session..." banner shows and sending is
|
|
217
|
+
// blocked until fresh history lands. resyncing is cleared by
|
|
218
|
+
// message_history (or an error/conflict) via the reducer.
|
|
219
|
+
this.stateStore.update(s => (s.resyncing ? s : {...s, resyncing: true}));
|
|
220
|
+
this.send({type: 'session_load', sessionFile: rejoinFile, cwd: rejoinCwd, model: rejoinModel});
|
|
183
221
|
this.armLoadWatchdog();
|
|
184
222
|
} else {
|
|
185
223
|
this.stateStore.update(s => (s.resyncing ? {...s, resyncing: false} : s));
|
|
@@ -212,6 +250,12 @@ export class WhereverClient {
|
|
|
212
250
|
// Never leave a session-load spinner stuck if the socket drops mid-load.
|
|
213
251
|
loadingSession: false,
|
|
214
252
|
creatingSession: false,
|
|
253
|
+
// If we still hold an active session, the drop is an unsolicited one on a
|
|
254
|
+
// live conversation: mark it resyncing NOW (during the reconnect backoff)
|
|
255
|
+
// so the user immediately sees "Reconnecting..." instead of a frozen view
|
|
256
|
+
// with a stale tool call and a disabled Abort. onOpen re-attaches and the
|
|
257
|
+
// reducer clears resyncing when fresh history lands.
|
|
258
|
+
resyncing: s.activeSessionFile ? true : s.resyncing,
|
|
215
259
|
}));
|
|
216
260
|
this.scheduleReconnect();
|
|
217
261
|
};
|
|
@@ -250,6 +294,11 @@ export class WhereverClient {
|
|
|
250
294
|
this.agentEndTimeout = null;
|
|
251
295
|
}
|
|
252
296
|
this.stopLivenessWatchdog();
|
|
297
|
+
// The load/create affordances belong to the socket we are tearing down. If
|
|
298
|
+
// left armed they would later fire against a fresh (re)connected session's
|
|
299
|
+
// state, so cancel them here.
|
|
300
|
+
this.clearLoadWatchdog();
|
|
301
|
+
this.clearCreateWatchdog();
|
|
253
302
|
|
|
254
303
|
if (this.ws) {
|
|
255
304
|
try {
|
|
@@ -267,6 +316,22 @@ export class WhereverClient {
|
|
|
267
316
|
hideThinking: !!this.config.hideThinking,
|
|
268
317
|
hideTools: !!this.config.hideTools,
|
|
269
318
|
});
|
|
319
|
+
} else {
|
|
320
|
+
// Preserve-cache teardown (suspend / resume's pre-connect close). The
|
|
321
|
+
// socket is gone, so the store MUST reflect disconnected: leaving a stale
|
|
322
|
+
// connected:true made resume() early-return (its `if (connected) return`
|
|
323
|
+
// guard), so the real reconnect fell through to a plain connect() that did
|
|
324
|
+
// NOT preserve the session, which is what flashed the search / new-session
|
|
325
|
+
// empty-state on tab return. Clearing connected here (without wiping the
|
|
326
|
+
// cached session/messages) lets resume() actually rejoin in place, and
|
|
327
|
+
// never leaves the blocking load/create overlays up on a dead socket.
|
|
328
|
+
this.stateStore.update(s => ({
|
|
329
|
+
...s,
|
|
330
|
+
connected: false,
|
|
331
|
+
connecting: false,
|
|
332
|
+
loadingSession: false,
|
|
333
|
+
creatingSession: false,
|
|
334
|
+
}));
|
|
270
335
|
}
|
|
271
336
|
}
|
|
272
337
|
|
|
@@ -300,7 +365,11 @@ export class WhereverClient {
|
|
|
300
365
|
public resume() {
|
|
301
366
|
const s = get(this.stateStore);
|
|
302
367
|
if (s.connected || s.connecting) return;
|
|
303
|
-
|
|
368
|
+
// Rejoin whatever session we still hold: an explicit suspend() record OR the
|
|
369
|
+
// cached active session from an unsolicited drop. In BOTH cases onOpen
|
|
370
|
+
// re-issues session_load, so mark resyncing here to show the affordance and
|
|
371
|
+
// block sending until fresh history lands.
|
|
372
|
+
if (this.resumeSessionFile || s.activeSessionFile) {
|
|
304
373
|
this.stateStore.update(st => ({...st, resyncing: true}));
|
|
305
374
|
// Arm here too: if the socket never opens, onOpen never runs and the
|
|
306
375
|
// resync affordance would otherwise hang. onClose also clears it, but the
|
|
@@ -318,6 +387,14 @@ export class WhereverClient {
|
|
|
318
387
|
return this.resumeSessionFile !== null;
|
|
319
388
|
}
|
|
320
389
|
|
|
390
|
+
// True when the store still holds an active session to rejoin in place -- from
|
|
391
|
+
// an explicit suspend() OR an unsolicited drop that left activeSessionFile set.
|
|
392
|
+
// Callers use this to prefer the preserve-cache resume() path over a fresh,
|
|
393
|
+
// session-wiping connect() when returning to a still-live conversation.
|
|
394
|
+
public hasActiveSession(): boolean {
|
|
395
|
+
return this.resumeSessionFile !== null || get(this.stateStore).activeSessionFile !== null;
|
|
396
|
+
}
|
|
397
|
+
|
|
321
398
|
// Start (or restart) the stale-socket watchdog + keepalive for the current
|
|
322
399
|
// socket. A half-open connection emits no 'close'/'error', so without this the
|
|
323
400
|
// existing reconnect logic would never fire and the agent would hang forever.
|
|
@@ -434,6 +511,37 @@ export class WhereverClient {
|
|
|
434
511
|
}
|
|
435
512
|
}
|
|
436
513
|
|
|
514
|
+
// Arm (or re-arm) the create watchdog. Called when session_new is issued so an
|
|
515
|
+
// unanswered create can never strand the blocking "Creating session..."
|
|
516
|
+
// overlay. On expiry we clear creatingSession and surface a recoverable error
|
|
517
|
+
// instead of an endless spin that only a reload escapes.
|
|
518
|
+
private armCreateWatchdog() {
|
|
519
|
+
this.clearCreateWatchdog();
|
|
520
|
+
this.createWatchdog = setTimeout(() => {
|
|
521
|
+
this.createWatchdog = null;
|
|
522
|
+
const s = get(this.stateStore);
|
|
523
|
+
if (!s.creatingSession) return;
|
|
524
|
+
console.warn('[wherever] session create timed out; clearing creating state');
|
|
525
|
+
this.stateStore.update(st => ({
|
|
526
|
+
...st,
|
|
527
|
+
creatingSession: false,
|
|
528
|
+
sessionError:
|
|
529
|
+
st.sessionError ||
|
|
530
|
+
'Creating the session timed out. It may still be starting on the server; check the sidebar or reload.',
|
|
531
|
+
}));
|
|
532
|
+
}, WhereverClient.CREATE_WATCHDOG_MS);
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
// Clear the create watchdog. Called at every point a create resolves
|
|
536
|
+
// (session_created, session_error, session_conflict, session_interrupted,
|
|
537
|
+
// disconnect, leave).
|
|
538
|
+
private clearCreateWatchdog() {
|
|
539
|
+
if (this.createWatchdog) {
|
|
540
|
+
clearTimeout(this.createWatchdog);
|
|
541
|
+
this.createWatchdog = null;
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
|
|
437
545
|
private scheduleReconnect() {
|
|
438
546
|
if (this.reconnectTimer) return;
|
|
439
547
|
|
|
@@ -441,14 +549,23 @@ export class WhereverClient {
|
|
|
441
549
|
this.reconnectTimer = null;
|
|
442
550
|
this.reconnectDelay = Math.min(this.reconnectDelay * 1.5, this.maxReconnectDelay);
|
|
443
551
|
console.info(`[wherever] reconnecting to relay (attempt ${++this.reconnectAttempts})`);
|
|
444
|
-
|
|
552
|
+
|
|
553
|
+
// If we still hold an active session, PRESERVE the cached store across the
|
|
554
|
+
// reconnect. A default connect() resets to a blank store, which wiped
|
|
555
|
+
// activeSessionFile BEFORE onOpen could re-attach to it -- that is what
|
|
556
|
+
// silently detached the frontend from a still-running session (frozen
|
|
557
|
+
// conversation, no re-follow, not even a "connecting"/"loading" hint).
|
|
558
|
+
// Preserving the store keeps the conversation on screen and lets onOpen
|
|
559
|
+
// re-issue session_load from activeSessionFile.
|
|
560
|
+
const preserve = !!get(this.stateStore).activeSessionFile;
|
|
561
|
+
|
|
445
562
|
this.stateStore.update(s => ({
|
|
446
563
|
...s,
|
|
447
564
|
connecting: true,
|
|
448
565
|
error: 'Reconnecting...',
|
|
449
566
|
}));
|
|
450
|
-
|
|
451
|
-
this.connect();
|
|
567
|
+
|
|
568
|
+
this.connect(undefined, preserve);
|
|
452
569
|
}, this.reconnectDelay);
|
|
453
570
|
}
|
|
454
571
|
|
|
@@ -474,6 +591,18 @@ export class WhereverClient {
|
|
|
474
591
|
break;
|
|
475
592
|
}
|
|
476
593
|
|
|
594
|
+
// Any message that resolves an in-flight session_new disarms the create
|
|
595
|
+
// watchdog. session_created is the success path; error/conflict/interrupt end
|
|
596
|
+
// it too.
|
|
597
|
+
switch (msg.type) {
|
|
598
|
+
case 'session_created':
|
|
599
|
+
case 'session_error':
|
|
600
|
+
case 'session_conflict':
|
|
601
|
+
case 'session_interrupted':
|
|
602
|
+
this.clearCreateWatchdog();
|
|
603
|
+
break;
|
|
604
|
+
}
|
|
605
|
+
|
|
477
606
|
switch (msg.type) {
|
|
478
607
|
case 'connected':
|
|
479
608
|
this.stateStore.update((s: WhereverState) => ({
|
|
@@ -778,12 +907,35 @@ export class WhereverClient {
|
|
|
778
907
|
// The server forces read-only for sessions in a configured
|
|
779
908
|
// sessions.readOnly folder (observe-only fleet view).
|
|
780
909
|
readOnly: msg.readOnly ?? false,
|
|
910
|
+
// pending -> the history is painted now but the live agent is still
|
|
911
|
+
// building; keep the composer disabled (agentPending) until
|
|
912
|
+
// session_ready. A non-pending create (new session, warm reload) is
|
|
913
|
+
// immediately sendable.
|
|
914
|
+
agentPending: msg.pending === true,
|
|
781
915
|
// Initial context-usage snapshot for the new session (null if unknown;
|
|
782
916
|
// live updates arrive via 'context_usage').
|
|
783
917
|
contextUsage: msg.contextUsage ?? null,
|
|
784
918
|
}));
|
|
785
919
|
break;
|
|
786
920
|
|
|
921
|
+
case 'session_ready':
|
|
922
|
+
// The live agent finished building for a previously-pending load. Enable
|
|
923
|
+
// the composer and refresh the now-authoritative model/streaming/usage.
|
|
924
|
+
this.stateStore.update((s: WhereverState) => {
|
|
925
|
+
// Ignore a stale ready for a session we already switched away from.
|
|
926
|
+
if (msg.sessionFile && s.activeSessionFile && msg.sessionFile !== s.activeSessionFile) {
|
|
927
|
+
return s;
|
|
928
|
+
}
|
|
929
|
+
return {
|
|
930
|
+
...s,
|
|
931
|
+
agentPending: false,
|
|
932
|
+
activeModel: msg.model ?? s.activeModel,
|
|
933
|
+
isStreaming: msg.isStreaming ?? s.isStreaming,
|
|
934
|
+
contextUsage: msg.contextUsage ?? s.contextUsage,
|
|
935
|
+
};
|
|
936
|
+
});
|
|
937
|
+
break;
|
|
938
|
+
|
|
787
939
|
case 'context_usage':
|
|
788
940
|
this.stateStore.update((s: WhereverState) => ({
|
|
789
941
|
...s,
|
|
@@ -803,6 +955,7 @@ export class WhereverClient {
|
|
|
803
955
|
activeCwd: null,
|
|
804
956
|
activeModel: null,
|
|
805
957
|
loadingSession: false,
|
|
958
|
+
agentPending: false,
|
|
806
959
|
};
|
|
807
960
|
}
|
|
808
961
|
return s;
|
|
@@ -817,6 +970,10 @@ export class WhereverClient {
|
|
|
817
970
|
creatingSession: false,
|
|
818
971
|
loadingSession: false,
|
|
819
972
|
resyncing: false,
|
|
973
|
+
// A failed (cold) agent build clears pending: the session stays
|
|
974
|
+
// readable, but sending remains blocked (no live agent) and the error
|
|
975
|
+
// is surfaced. This is the degrade-to-read-only path.
|
|
976
|
+
agentPending: false,
|
|
820
977
|
}));
|
|
821
978
|
break;
|
|
822
979
|
|
|
@@ -831,6 +988,7 @@ export class WhereverClient {
|
|
|
831
988
|
creatingSession: false,
|
|
832
989
|
loadingSession: false,
|
|
833
990
|
resyncing: false,
|
|
991
|
+
agentPending: false,
|
|
834
992
|
}));
|
|
835
993
|
break;
|
|
836
994
|
|
|
@@ -847,6 +1005,7 @@ export class WhereverClient {
|
|
|
847
1005
|
activeModel: null,
|
|
848
1006
|
creatingSession: false,
|
|
849
1007
|
loadingSession: false,
|
|
1008
|
+
agentPending: false,
|
|
850
1009
|
}));
|
|
851
1010
|
break;
|
|
852
1011
|
|
|
@@ -1027,6 +1186,20 @@ export class WhereverClient {
|
|
|
1027
1186
|
const s = get(this.stateStore);
|
|
1028
1187
|
if (!s.sessionId) return false;
|
|
1029
1188
|
|
|
1189
|
+
// The session's transcript is loaded but its live agent is still building
|
|
1190
|
+
// (cold, fast-first load). Reading is fine; sending is not possible yet, and
|
|
1191
|
+
// a message sent now would have no agent to receive it. Surface a clear,
|
|
1192
|
+
// recoverable hint and keep the text (the composer is disabled anyway, but a
|
|
1193
|
+
// programmatic caller could still reach here).
|
|
1194
|
+
if (s.agentPending) {
|
|
1195
|
+
this.stateStore.update((st: WhereverState) => ({
|
|
1196
|
+
...st,
|
|
1197
|
+
sessionError:
|
|
1198
|
+
st.sessionError ?? 'Preparing the session agent; please wait a moment, then resend.',
|
|
1199
|
+
}));
|
|
1200
|
+
return false;
|
|
1201
|
+
}
|
|
1202
|
+
|
|
1030
1203
|
// Guard against sending on a dead / half-open socket. getIsConnected()
|
|
1031
1204
|
// checks the real readyState (not the store's connected flag, which can lag
|
|
1032
1205
|
// a half-open socket the liveness watchdog has not reaped yet). Sending here
|
|
@@ -1087,6 +1260,7 @@ export class WhereverClient {
|
|
|
1087
1260
|
conflict: null,
|
|
1088
1261
|
sessionError: null,
|
|
1089
1262
|
loadingSession: true,
|
|
1263
|
+
agentPending: false,
|
|
1090
1264
|
}));
|
|
1091
1265
|
this.send({type: 'session_load', sessionFile, cwd, model});
|
|
1092
1266
|
this.armLoadWatchdog();
|
|
@@ -1125,12 +1299,16 @@ export class WhereverClient {
|
|
|
1125
1299
|
sessionError: null,
|
|
1126
1300
|
creatingSession: false,
|
|
1127
1301
|
resyncing: false,
|
|
1302
|
+
agentPending: false,
|
|
1128
1303
|
contextUsage: null,
|
|
1129
1304
|
loadingSession: true,
|
|
1130
1305
|
}));
|
|
1131
1306
|
this.resumeSessionFile = null;
|
|
1132
1307
|
this.resumeCwd = undefined;
|
|
1133
1308
|
this.resumeModel = undefined;
|
|
1309
|
+
// Switching supersedes any in-flight create; disarm its watchdog so it can't
|
|
1310
|
+
// later fire against the newly loaded session.
|
|
1311
|
+
this.clearCreateWatchdog();
|
|
1134
1312
|
this.send({type: 'session_load', sessionFile, cwd, model});
|
|
1135
1313
|
this.armLoadWatchdog();
|
|
1136
1314
|
}
|
|
@@ -1157,11 +1335,25 @@ export class WhereverClient {
|
|
|
1157
1335
|
createRemote,
|
|
1158
1336
|
repoVisibility,
|
|
1159
1337
|
});
|
|
1338
|
+
// Never let the blocking "Creating session..." overlay hang if the reply is
|
|
1339
|
+
// lost. Symmetrical with armLoadWatchdog for session_load.
|
|
1340
|
+
this.armCreateWatchdog();
|
|
1160
1341
|
}
|
|
1161
1342
|
|
|
1162
1343
|
public leaveSession() {
|
|
1163
1344
|
const s = get(this.stateStore);
|
|
1164
|
-
|
|
1345
|
+
// Leaving abandons any in-flight create too, so disarm its watchdog even when
|
|
1346
|
+
// there is no sessionId yet (a create that has not resolved).
|
|
1347
|
+
this.clearCreateWatchdog();
|
|
1348
|
+
if (!s.sessionId) {
|
|
1349
|
+
if (s.creatingSession) {
|
|
1350
|
+
this.stateStore.update((st: WhereverState) => ({
|
|
1351
|
+
...st,
|
|
1352
|
+
creatingSession: false,
|
|
1353
|
+
}));
|
|
1354
|
+
}
|
|
1355
|
+
return;
|
|
1356
|
+
}
|
|
1165
1357
|
this.send({type: 'session_leave', sessionId: s.sessionId});
|
|
1166
1358
|
this.stateStore.update((s: WhereverState) => ({
|
|
1167
1359
|
...s,
|
|
@@ -1175,6 +1367,7 @@ export class WhereverClient {
|
|
|
1175
1367
|
creatingSession: false,
|
|
1176
1368
|
loadingSession: false,
|
|
1177
1369
|
resyncing: false,
|
|
1370
|
+
agentPending: false,
|
|
1178
1371
|
contextUsage: null,
|
|
1179
1372
|
}));
|
|
1180
1373
|
this.clearLoadWatchdog();
|
package/src/types.ts
CHANGED
|
@@ -31,6 +31,13 @@ export interface WhereverState {
|
|
|
31
31
|
// messages remain visible; the UI shows a "reconnecting/syncing" affordance
|
|
32
32
|
// and blocks input until the resync completes.
|
|
33
33
|
resyncing: boolean;
|
|
34
|
+
// True after a `pending` session_created (history painted from a cheap read)
|
|
35
|
+
// until session_ready arrives (the live agent finished building). The
|
|
36
|
+
// conversation is READABLE during this window, but SENDING must be blocked:
|
|
37
|
+
// reading only needs the transcript, sending needs the live agent. Distinct
|
|
38
|
+
// from loadingSession/resyncing, which gate the whole view; agentPending gates
|
|
39
|
+
// only the composer so opening a cold session to read is instant.
|
|
40
|
+
agentPending: boolean;
|
|
34
41
|
error: string | null;
|
|
35
42
|
session: string | null;
|
|
36
43
|
sessionId: string | null;
|
|
@@ -0,0 +1,107 @@
|
|
|
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
|
+
// Fast-first load reducer contract: a `pending` session_created paints history
|
|
7
|
+
// but keeps the composer blocked (agentPending) until session_ready; a
|
|
8
|
+
// non-pending create is immediately sendable; a session_error while pending
|
|
9
|
+
// degrades to readable-but-not-sendable.
|
|
10
|
+
|
|
11
|
+
describe('agentPending (fast-first load)', () => {
|
|
12
|
+
let ws: ReturnType<typeof makeWSFactory>;
|
|
13
|
+
let client: WhereverClient;
|
|
14
|
+
|
|
15
|
+
beforeEach(() => {
|
|
16
|
+
vi.useFakeTimers();
|
|
17
|
+
ws = makeWSFactory();
|
|
18
|
+
client = new WhereverClient({
|
|
19
|
+
host: 'localhost',
|
|
20
|
+
port: 1234,
|
|
21
|
+
secure: false,
|
|
22
|
+
WebSocketCtor: ws.ctor,
|
|
23
|
+
});
|
|
24
|
+
client.connect();
|
|
25
|
+
ws.last().open();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
afterEach(() => {
|
|
29
|
+
client.disconnect(true);
|
|
30
|
+
vi.useRealTimers();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
function loadPending() {
|
|
34
|
+
client.joinSession('/tmp/p/session.jsonl');
|
|
35
|
+
ws.last().receive({
|
|
36
|
+
type: 'session_created',
|
|
37
|
+
sessionId: 'sid-1',
|
|
38
|
+
sessionFile: '/tmp/p/session.jsonl',
|
|
39
|
+
cwd: '/tmp/p',
|
|
40
|
+
model: '',
|
|
41
|
+
isStreaming: false,
|
|
42
|
+
pending: true,
|
|
43
|
+
});
|
|
44
|
+
ws.last().receive({
|
|
45
|
+
type: 'message_history',
|
|
46
|
+
sessionId: 'sid-1',
|
|
47
|
+
messages: [
|
|
48
|
+
{role: 'user', content: 'hi', timestamp: 1},
|
|
49
|
+
{role: 'assistant', content: 'hello', timestamp: 2},
|
|
50
|
+
],
|
|
51
|
+
totalCount: 2,
|
|
52
|
+
offset: 0,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
it('paints history but stays agentPending until session_ready', () => {
|
|
57
|
+
loadPending();
|
|
58
|
+
let s = get(client.stateStore);
|
|
59
|
+
// History is readable now.
|
|
60
|
+
expect(s.messages.length).toBe(2);
|
|
61
|
+
expect(s.loadingSession).toBe(false);
|
|
62
|
+
// But sending is blocked until the agent is ready.
|
|
63
|
+
expect(s.agentPending).toBe(true);
|
|
64
|
+
expect(client.sendMessage('nope')).toBe(false);
|
|
65
|
+
|
|
66
|
+
ws.last().receive({
|
|
67
|
+
type: 'session_ready',
|
|
68
|
+
sessionId: 'sid-1',
|
|
69
|
+
sessionFile: '/tmp/p/session.jsonl',
|
|
70
|
+
model: 'fake:model',
|
|
71
|
+
isStreaming: false,
|
|
72
|
+
});
|
|
73
|
+
s = get(client.stateStore);
|
|
74
|
+
expect(s.agentPending).toBe(false);
|
|
75
|
+
expect(s.activeModel).toBe('fake:model');
|
|
76
|
+
// Now a send goes out over the open socket.
|
|
77
|
+
expect(client.sendMessage('now it works')).toBe(true);
|
|
78
|
+
expect(ws.last().lastSentOfType('message')?.message).toBe('now it works');
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('a non-pending create is immediately sendable', () => {
|
|
82
|
+
client.createSession('/tmp/new');
|
|
83
|
+
ws.last().receive({
|
|
84
|
+
type: 'session_created',
|
|
85
|
+
sessionId: 'sid-2',
|
|
86
|
+
sessionFile: '/tmp/new/session.jsonl',
|
|
87
|
+
cwd: '/tmp/new',
|
|
88
|
+
model: 'fake:model',
|
|
89
|
+
isStreaming: false,
|
|
90
|
+
// no pending flag
|
|
91
|
+
});
|
|
92
|
+
const s = get(client.stateStore);
|
|
93
|
+
expect(s.agentPending).toBe(false);
|
|
94
|
+
expect(client.sendMessage('hi')).toBe(true);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('session_error while pending degrades to readable-but-not-sendable', () => {
|
|
98
|
+
loadPending();
|
|
99
|
+
expect(get(client.stateStore).agentPending).toBe(true);
|
|
100
|
+
ws.last().receive({type: 'session_error', sessionId: 'sid-1', error: 'agent build failed'});
|
|
101
|
+
const s = get(client.stateStore);
|
|
102
|
+
expect(s.agentPending).toBe(false);
|
|
103
|
+
expect(s.sessionError).toBe('agent build failed');
|
|
104
|
+
// History is still there to read.
|
|
105
|
+
expect(s.messages.length).toBe(2);
|
|
106
|
+
});
|
|
107
|
+
});
|
|
@@ -0,0 +1,82 @@
|
|
|
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 1: creating a new session shows the full-screen "Creating session..."
|
|
7
|
+
// spinner (state.creatingSession) forever when the server never replies with
|
|
8
|
+
// session_created / session_error (a slow git init or remote-repo creation, an
|
|
9
|
+
// error thrown before the reply is sent, or a half-open socket the liveness
|
|
10
|
+
// watchdog has not reaped yet). Every OTHER load flag (loadingSession,
|
|
11
|
+
// resyncing) is guarded by a watchdog; creatingSession was not. These tests pin
|
|
12
|
+
// the contract: creatingSession must always resolve.
|
|
13
|
+
|
|
14
|
+
describe('creating-session watchdog', () => {
|
|
15
|
+
let ws: ReturnType<typeof makeWSFactory>;
|
|
16
|
+
let client: WhereverClient;
|
|
17
|
+
|
|
18
|
+
beforeEach(() => {
|
|
19
|
+
vi.useFakeTimers();
|
|
20
|
+
ws = makeWSFactory();
|
|
21
|
+
client = new WhereverClient({
|
|
22
|
+
host: 'localhost',
|
|
23
|
+
port: 1234,
|
|
24
|
+
secure: false,
|
|
25
|
+
WebSocketCtor: ws.ctor,
|
|
26
|
+
});
|
|
27
|
+
client.connect();
|
|
28
|
+
ws.last().open();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
afterEach(() => {
|
|
32
|
+
client.disconnect(true);
|
|
33
|
+
vi.useRealTimers();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('sets creatingSession true when a create is requested', () => {
|
|
37
|
+
client.createSession('/tmp/project');
|
|
38
|
+
expect(get(client.stateStore).creatingSession).toBe(true);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('clears creatingSession and surfaces a recoverable error if no reply ever arrives', () => {
|
|
42
|
+
client.createSession('/tmp/project');
|
|
43
|
+
expect(get(client.stateStore).creatingSession).toBe(true);
|
|
44
|
+
|
|
45
|
+
// No session_created / session_error comes back. Advance past the watchdog.
|
|
46
|
+
vi.advanceTimersByTime(30_000);
|
|
47
|
+
|
|
48
|
+
const s = get(client.stateStore);
|
|
49
|
+
expect(s.creatingSession).toBe(false);
|
|
50
|
+
expect(s.sessionError).toBeTruthy();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('clears creatingSession normally on session_created (watchdog does not fire)', () => {
|
|
54
|
+
client.createSession('/tmp/project');
|
|
55
|
+
ws.last().receive({
|
|
56
|
+
type: 'session_created',
|
|
57
|
+
sessionId: 'sid-1',
|
|
58
|
+
sessionFile: '/tmp/project/session.jsonl',
|
|
59
|
+
cwd: '/tmp/project',
|
|
60
|
+
model: 'fake:model',
|
|
61
|
+
isStreaming: false,
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
expect(get(client.stateStore).creatingSession).toBe(false);
|
|
65
|
+
|
|
66
|
+
// The watchdog must have been disarmed: advancing time must NOT resurrect
|
|
67
|
+
// an error on the now-successfully-created session.
|
|
68
|
+
vi.advanceTimersByTime(30_000);
|
|
69
|
+
const s = get(client.stateStore);
|
|
70
|
+
expect(s.creatingSession).toBe(false);
|
|
71
|
+
expect(s.sessionError).toBeFalsy();
|
|
72
|
+
expect(s.activeSessionFile).toBe('/tmp/project/session.jsonl');
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('clears creatingSession on session_error', () => {
|
|
76
|
+
client.createSession('/tmp/project');
|
|
77
|
+
ws.last().receive({type: 'session_error', error: 'boom'});
|
|
78
|
+
const s = get(client.stateStore);
|
|
79
|
+
expect(s.creatingSession).toBe(false);
|
|
80
|
+
expect(s.sessionError).toBe('boom');
|
|
81
|
+
});
|
|
82
|
+
});
|
package/test/harness.ts
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// Minimal fake WebSocket for driving WhereverClient.handleMessage deterministically
|
|
2
|
+
// in unit tests. It records outbound frames and lets a test push inbound frames
|
|
3
|
+
// (server -> client) synchronously. No real network. Mirrors the tiny slice of
|
|
4
|
+
// the WHATWG WebSocket API that the client uses (readyState, addEventListener for
|
|
5
|
+
// open/message/close/error, send, close).
|
|
6
|
+
|
|
7
|
+
export type WSListener = (event: any) => void;
|
|
8
|
+
|
|
9
|
+
export class FakeWebSocket {
|
|
10
|
+
// WHATWG readyState constants.
|
|
11
|
+
static CONNECTING = 0;
|
|
12
|
+
static OPEN = 1;
|
|
13
|
+
static CLOSING = 2;
|
|
14
|
+
static CLOSED = 3;
|
|
15
|
+
|
|
16
|
+
url: string;
|
|
17
|
+
readyState = FakeWebSocket.CONNECTING;
|
|
18
|
+
sent: any[] = [];
|
|
19
|
+
private listeners: Record<string, Set<WSListener>> = {
|
|
20
|
+
open: new Set(),
|
|
21
|
+
message: new Set(),
|
|
22
|
+
close: new Set(),
|
|
23
|
+
error: new Set(),
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
constructor(url: string) {
|
|
27
|
+
this.url = url;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
addEventListener(type: string, cb: WSListener) {
|
|
31
|
+
this.listeners[type]?.add(cb);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
removeEventListener(type: string, cb: WSListener) {
|
|
35
|
+
this.listeners[type]?.delete(cb);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
removeAllListeners() {
|
|
39
|
+
for (const key of Object.keys(this.listeners)) {
|
|
40
|
+
this.listeners[key].clear();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
send(data: string) {
|
|
45
|
+
this.sent.push(JSON.parse(data));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
close() {
|
|
49
|
+
if (this.readyState === FakeWebSocket.CLOSED) return;
|
|
50
|
+
this.readyState = FakeWebSocket.CLOSED;
|
|
51
|
+
this.emit('close', {});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// terminate() is used by the liveness watchdog's forcible teardown path.
|
|
55
|
+
terminate() {
|
|
56
|
+
this.close();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// --- test-only helpers -------------------------------------------------
|
|
60
|
+
|
|
61
|
+
/** Simulate the socket opening (server accepted the connection). */
|
|
62
|
+
open() {
|
|
63
|
+
this.readyState = FakeWebSocket.OPEN;
|
|
64
|
+
this.emit('open', {});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Push a server -> client frame. */
|
|
68
|
+
receive(msg: any) {
|
|
69
|
+
this.emit('message', {data: JSON.stringify(msg)});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Last outbound frame of a given type, or undefined. */
|
|
73
|
+
lastSentOfType(type: string) {
|
|
74
|
+
return [...this.sent].reverse().find((m) => m.type === type);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
private emit(type: string, event: any) {
|
|
78
|
+
for (const cb of this.listeners[type] ?? []) cb(event);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// A WebSocket constructor that records every instance it creates, so a test can
|
|
83
|
+
// grab the live socket the client just opened and drive it. Assigned to
|
|
84
|
+
// config.WebSocketCtor.
|
|
85
|
+
export function makeWSFactory() {
|
|
86
|
+
const instances: FakeWebSocket[] = [];
|
|
87
|
+
const ctor = function (url: string) {
|
|
88
|
+
const ws = new FakeWebSocket(url);
|
|
89
|
+
instances.push(ws);
|
|
90
|
+
return ws;
|
|
91
|
+
} as unknown as {new (url: string): FakeWebSocket};
|
|
92
|
+
return {
|
|
93
|
+
ctor,
|
|
94
|
+
instances,
|
|
95
|
+
last: () => instances[instances.length - 1],
|
|
96
|
+
};
|
|
97
|
+
}
|