@clawling/clawchat-plugin-openclaw 2026.8.26-1 → 2026.8.27-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/README.md +3 -3
- package/dist/src/runtime.js +13 -7
- package/dist/src/ws-client.js +102 -12
- package/package.json +1 -1
- package/src/runtime.ts +13 -8
- package/src/ws-client.ts +103 -12
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@ OpenClaw channel plugin that connects an agent to ClawChat over ClawChat Protoco
|
|
|
6
6
|
|
|
7
7
|
- Plugin-owned WebSocket transport with auto-reconnect (exponential backoff + jitter), heartbeat, and ack tracking
|
|
8
8
|
- Inbound `message.send` / `message.reply` with reply context
|
|
9
|
-
- Outbound text replies
|
|
9
|
+
- Outbound text replies as complete `message.send` / `message.reply` frames — the plugin consumes inbound streams but never emits streaming lifecycle frames
|
|
10
10
|
- Typing indicators and filtered forwarding for thinking / tool-call content
|
|
11
11
|
- Media fragments (image / file / audio / video) in either direction
|
|
12
12
|
- Invite-code onboarding (no raw credentials) via `/clawchat-activate` or supported `openclaw channels add`, plus always-registered `clawchat_*` account/media tools
|
|
@@ -95,7 +95,7 @@ src/
|
|
|
95
95
|
per-group idle/max batching for non-mention turns
|
|
96
96
|
group-settings.ts per-group agent settings cache (GET /v1/agents/me/
|
|
97
97
|
group-settings)
|
|
98
|
-
reply-dispatcher.ts
|
|
98
|
+
reply-dispatcher.ts outbound reply routing (complete frames only)
|
|
99
99
|
no-reply.ts clawchat:no-reply / :silent suppression token guard
|
|
100
100
|
login.runtime.ts invite-code exchange flow
|
|
101
101
|
refresh-manager.ts ClawChat token refresh + auto-logout orchestration
|
|
@@ -153,7 +153,7 @@ The main reference is
|
|
|
153
153
|
- Full configuration reference
|
|
154
154
|
- Onboarding / activation details
|
|
155
155
|
- REST endpoint table
|
|
156
|
-
-
|
|
156
|
+
- Inbound streaming frame shapes (`message.created` / `message.add` / `message.done` / `message.reply`) — consumed, never produced
|
|
157
157
|
- End-to-end sequence diagram
|
|
158
158
|
- Media pipeline (inbound download / outbound upload)
|
|
159
159
|
- Troubleshooting
|
package/dist/src/runtime.js
CHANGED
|
@@ -108,20 +108,26 @@ function isRecord(value) {
|
|
|
108
108
|
}
|
|
109
109
|
/**
|
|
110
110
|
* §A.2 — classify a WS `hello-fail` reason for refresh gating.
|
|
111
|
-
* - "token-rejected":
|
|
111
|
+
* - "token-rejected": the ONE terminal reason → refresh.
|
|
112
112
|
* - "auth-unavailable": 5xx auth-backend outage → backoff, DO NOT refresh.
|
|
113
|
-
* - "generic":
|
|
113
|
+
* - "generic": anything else → transient; backoff with the current token.
|
|
114
114
|
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
115
|
+
* msghub Protocol v2 §3.5 pins the matching contract: `"authentication failed"`
|
|
116
|
+
* is matched by exact equality (case / surrounding whitespace tolerant, like the
|
|
117
|
+
* hermes plugin's `_is_token_rejected`), never by substring or prefix, and no
|
|
118
|
+
* speculative matchers (`invalid token`, `token expired`, `unauthorized`, …) may
|
|
119
|
+
* be added — msghub does not emit them, and matching them would turn a reason
|
|
120
|
+
* added later into a forced refresh and, on a rotated refresh token, a re-pair.
|
|
121
|
+
* The ws-client already routes every non-terminal reason as a TransportError
|
|
122
|
+
* (backoff), so only the exact terminal string normally reaches this classifier;
|
|
123
|
+
* it stays defensive for the live-session path.
|
|
117
124
|
*/
|
|
118
125
|
export function classifyHelloFailReason(reason) {
|
|
119
|
-
const r = (reason || "").toLowerCase();
|
|
126
|
+
const r = (reason || "").trim().toLowerCase();
|
|
120
127
|
if (/auth service unavailable|temporarily unavailable/.test(r))
|
|
121
128
|
return "auth-unavailable";
|
|
122
|
-
if (
|
|
129
|
+
if (r === "authentication failed")
|
|
123
130
|
return "token-rejected";
|
|
124
|
-
}
|
|
125
131
|
return "generic";
|
|
126
132
|
}
|
|
127
133
|
/** Read `channels.<CHANNEL_ID>.refreshToken` from a live config, or null. */
|
package/dist/src/ws-client.js
CHANGED
|
@@ -185,6 +185,62 @@ export function isValidChatId(chatId) {
|
|
|
185
185
|
}
|
|
186
186
|
return true;
|
|
187
187
|
}
|
|
188
|
+
/**
|
|
189
|
+
* §3.5 terminal `hello-fail` reason. Exact equality after trimming and
|
|
190
|
+
* lower-casing (parity with the hermes plugin's `_is_token_rejected`); never a
|
|
191
|
+
* substring or prefix match — see `onHelloFail`.
|
|
192
|
+
*/
|
|
193
|
+
export const TERMINAL_HELLO_FAIL_REASON = "authentication failed";
|
|
194
|
+
export function isTerminalHelloFailReason(reason) {
|
|
195
|
+
return (reason ?? "").trim().toLowerCase() === TERMINAL_HELLO_FAIL_REASON;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* §3.6 duplicate-session refusal. msghub turns away the NEWER connection with
|
|
199
|
+
* this close code and a JSON reason `{reason, retry_after_ms}` when the opt-in
|
|
200
|
+
* guard is on. The server owns the 60s → 300s escalation, so the client only
|
|
201
|
+
* has to wait the value it is handed.
|
|
202
|
+
*/
|
|
203
|
+
export const CLOSE_CODE_DUPLICATE_THROTTLED = 4002;
|
|
204
|
+
/** Floor used when a 4002 close carries no parseable retry_after_ms. */
|
|
205
|
+
export const DEFAULT_REFUSAL_FLOOR_MS = 60_000;
|
|
206
|
+
/**
|
|
207
|
+
* §3.6 duplicate-session takeover. msghub closes the OLDER socket with 4001
|
|
208
|
+
* when a newer session for the same (user, device) takes over. Reconnecting at
|
|
209
|
+
* the ordinary initial delay races that session and kicks it back — the
|
|
210
|
+
* mutual-eviction storm.
|
|
211
|
+
*
|
|
212
|
+
* A single eviction waits the base floor; consecutive evictions of the same
|
|
213
|
+
* connection escalate it (doubling, capped), so whichever instance keeps losing
|
|
214
|
+
* withdraws further each round until the other simply stays connected. Prod
|
|
215
|
+
* measured 9–163 s per eviction (msghub 2026-08-18), longer than the base, so a
|
|
216
|
+
* flat floor cannot break a persistent loop — only the escalation does. The
|
|
217
|
+
* streak survives `hello-ok` (an eviction happens *after* we connect) and is
|
|
218
|
+
* reset only by a non-4001 close, mirroring the hermes plugin.
|
|
219
|
+
*/
|
|
220
|
+
export const CLOSE_CODE_REPLACED = 4001;
|
|
221
|
+
/** Base floor for the first takeover eviction (the §3.6 minimum). */
|
|
222
|
+
export const TAKEOVER_FLOOR_MS = 5_000;
|
|
223
|
+
/** Ceiling for the escalating takeover floor. */
|
|
224
|
+
export const TAKEOVER_FLOOR_MAX_MS = 600_000;
|
|
225
|
+
/** Reconnect floor for `streak` consecutive 4001 evictions (0 → no floor). */
|
|
226
|
+
export function takeoverFloorMs(streak) {
|
|
227
|
+
if (streak <= 0)
|
|
228
|
+
return 0;
|
|
229
|
+
return Math.min(TAKEOVER_FLOOR_MS * 2 ** (streak - 1), TAKEOVER_FLOOR_MAX_MS);
|
|
230
|
+
}
|
|
231
|
+
export function parseRefusalRetryAfterMs(reason) {
|
|
232
|
+
if (reason) {
|
|
233
|
+
try {
|
|
234
|
+
const value = JSON.parse(reason).retry_after_ms;
|
|
235
|
+
if (typeof value === "number" && Number.isFinite(value) && value > 0)
|
|
236
|
+
return value;
|
|
237
|
+
}
|
|
238
|
+
catch {
|
|
239
|
+
// fall through to the default floor
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return DEFAULT_REFUSAL_FLOOR_MS;
|
|
243
|
+
}
|
|
188
244
|
export class ClawChatClient extends EventEmitter {
|
|
189
245
|
opts;
|
|
190
246
|
currentState = "idle";
|
|
@@ -194,6 +250,10 @@ export class ClawChatClient extends EventEmitter {
|
|
|
194
250
|
pongTimer;
|
|
195
251
|
reconnectTimer;
|
|
196
252
|
reconnectAttempts = 0;
|
|
253
|
+
// Consecutive 4001 duplicate-session evictions. Unlike reconnectAttempts it
|
|
254
|
+
// is NOT reset on hello-ok (the eviction lands after we connect); a non-4001
|
|
255
|
+
// close resets it. Drives the escalating takeover floor (§3.6).
|
|
256
|
+
takeoverStreak = 0;
|
|
197
257
|
closing = false;
|
|
198
258
|
authFailed = false;
|
|
199
259
|
expectedConnectTraceId;
|
|
@@ -664,13 +724,16 @@ export class ClawChatClient extends EventEmitter {
|
|
|
664
724
|
this.failHandshake(new ProtocolError("invalid hello-fail payload", env), 4002, "protocol error");
|
|
665
725
|
return;
|
|
666
726
|
}
|
|
667
|
-
// §
|
|
668
|
-
//
|
|
669
|
-
//
|
|
670
|
-
//
|
|
671
|
-
//
|
|
672
|
-
//
|
|
673
|
-
|
|
727
|
+
// §3.5: `"authentication failed"` — matched by EXACT equality, never a
|
|
728
|
+
// substring — is the one terminal reason (the token was rejected by upstream
|
|
729
|
+
// auth). Every other reason is transient: `remote auth service unavailable`
|
|
730
|
+
// (the auth backend is down, the token may still be valid — §14.1), `nonce
|
|
731
|
+
// mismatch`, `invalid connect …`, and any string msghub adds after this
|
|
732
|
+
// client shipped. For all of those: backoff-reconnect with the SAME token,
|
|
733
|
+
// keep the outbound queue, and do NOT refresh (a 5xx storm must not become
|
|
734
|
+
// a mass token-refresh storm; a new server-side reason must not brick old
|
|
735
|
+
// clients). Widening the terminal branch breaks that forward-compat rule.
|
|
736
|
+
if (!isTerminalHelloFailReason(reason)) {
|
|
674
737
|
const err = new TransportError(reason);
|
|
675
738
|
this.expectedConnectTraceId = undefined;
|
|
676
739
|
this.clearTimers();
|
|
@@ -680,8 +743,12 @@ export class ClawChatClient extends EventEmitter {
|
|
|
680
743
|
this.connectReject = undefined;
|
|
681
744
|
this.emitError(err);
|
|
682
745
|
if (this.opts.transport.state !== "closed") {
|
|
683
|
-
// Close WITHOUT marking closing/authFailed so handleClose
|
|
684
|
-
|
|
746
|
+
// Close WITHOUT marking closing/authFailed so handleClose
|
|
747
|
+
// backoff-reconnects. Use 1000, NOT 4001: 4001 is the server's
|
|
748
|
+
// duplicate-session takeover code that handleClose now floors to 5s
|
|
749
|
+
// (§3.6), and this client-initiated transient teardown must reconnect
|
|
750
|
+
// on the ordinary backoff instead of inheriting that floor.
|
|
751
|
+
this.opts.transport.close(1000, reason);
|
|
685
752
|
}
|
|
686
753
|
else if (!this.closing && this.opts.reconnect.enabled) {
|
|
687
754
|
this.scheduleReconnect(reason);
|
|
@@ -796,7 +863,27 @@ export class ClawChatClient extends EventEmitter {
|
|
|
796
863
|
this.connectReject?.(closeError);
|
|
797
864
|
return;
|
|
798
865
|
}
|
|
799
|
-
|
|
866
|
+
// §3.6: raise an explicit reconnect floor for the two server-timed
|
|
867
|
+
// duplicate-session closes. 4002 (refusal) waits the server's
|
|
868
|
+
// retry_after_ms, which exceeds the maxDelay cap; 4001 (takeover) waits at
|
|
869
|
+
// least 5s so an instant reconnect doesn't re-enter the eviction storm.
|
|
870
|
+
let floorMs;
|
|
871
|
+
if (code === CLOSE_CODE_DUPLICATE_THROTTLED) {
|
|
872
|
+
// A refusal is not a takeover-loss — reset the streak and wait the
|
|
873
|
+
// server's retry_after_ms.
|
|
874
|
+
this.takeoverStreak = 0;
|
|
875
|
+
floorMs = parseRefusalRetryAfterMs(reason);
|
|
876
|
+
}
|
|
877
|
+
else if (code === CLOSE_CODE_REPLACED) {
|
|
878
|
+
this.takeoverStreak += 1;
|
|
879
|
+
floorMs = takeoverFloorMs(this.takeoverStreak);
|
|
880
|
+
}
|
|
881
|
+
else {
|
|
882
|
+
// Any other close (ordinary drop, backpressure kick) is not an eviction
|
|
883
|
+
// loss — reset so the next takeover starts from the base floor again.
|
|
884
|
+
this.takeoverStreak = 0;
|
|
885
|
+
}
|
|
886
|
+
this.scheduleReconnect(reason || `close ${code}`, floorMs);
|
|
800
887
|
}
|
|
801
888
|
failHandshake(err, code, reason) {
|
|
802
889
|
this.clearTimers();
|
|
@@ -813,7 +900,7 @@ export class ClawChatClient extends EventEmitter {
|
|
|
813
900
|
this.opts.transport.close(code, reason);
|
|
814
901
|
}
|
|
815
902
|
}
|
|
816
|
-
scheduleReconnect(reason) {
|
|
903
|
+
scheduleReconnect(reason, floorMs) {
|
|
817
904
|
if (this.reconnectTimer)
|
|
818
905
|
return;
|
|
819
906
|
if (this.reconnectAttempts >= this.opts.reconnect.maxRetries) {
|
|
@@ -823,7 +910,10 @@ export class ClawChatClient extends EventEmitter {
|
|
|
823
910
|
return;
|
|
824
911
|
}
|
|
825
912
|
this.reconnectAttempts += 1;
|
|
826
|
-
const
|
|
913
|
+
const cappedDelay = Math.min(this.opts.reconnect.maxDelay, this.opts.reconnect.initialDelay * 2 ** Math.max(0, this.reconnectAttempts - 1));
|
|
914
|
+
// An explicit floor (a 4002 retry_after_ms) overrides the maxDelay cap: the
|
|
915
|
+
// server's wait is deliberately longer than the ordinary backoff ceiling.
|
|
916
|
+
const baseDelay = floorMs !== undefined ? Math.max(cappedDelay, floorMs) : cappedDelay;
|
|
827
917
|
const jitter = baseDelay * this.opts.reconnect.jitterRatio * Math.random();
|
|
828
918
|
const delay = Math.round(baseDelay + jitter);
|
|
829
919
|
this.transition("reconnecting");
|
package/package.json
CHANGED
package/src/runtime.ts
CHANGED
|
@@ -210,21 +210,26 @@ function isRecord(value: unknown): value is Record<string, unknown> {
|
|
|
210
210
|
|
|
211
211
|
/**
|
|
212
212
|
* §A.2 — classify a WS `hello-fail` reason for refresh gating.
|
|
213
|
-
* - "token-rejected":
|
|
213
|
+
* - "token-rejected": the ONE terminal reason → refresh.
|
|
214
214
|
* - "auth-unavailable": 5xx auth-backend outage → backoff, DO NOT refresh.
|
|
215
|
-
* - "generic":
|
|
215
|
+
* - "generic": anything else → transient; backoff with the current token.
|
|
216
216
|
*
|
|
217
|
-
*
|
|
218
|
-
*
|
|
217
|
+
* msghub Protocol v2 §3.5 pins the matching contract: `"authentication failed"`
|
|
218
|
+
* is matched by exact equality (case / surrounding whitespace tolerant, like the
|
|
219
|
+
* hermes plugin's `_is_token_rejected`), never by substring or prefix, and no
|
|
220
|
+
* speculative matchers (`invalid token`, `token expired`, `unauthorized`, …) may
|
|
221
|
+
* be added — msghub does not emit them, and matching them would turn a reason
|
|
222
|
+
* added later into a forced refresh and, on a rotated refresh token, a re-pair.
|
|
223
|
+
* The ws-client already routes every non-terminal reason as a TransportError
|
|
224
|
+
* (backoff), so only the exact terminal string normally reaches this classifier;
|
|
225
|
+
* it stays defensive for the live-session path.
|
|
219
226
|
*/
|
|
220
227
|
export function classifyHelloFailReason(
|
|
221
228
|
reason: string,
|
|
222
229
|
): "token-rejected" | "auth-unavailable" | "generic" {
|
|
223
|
-
const r = (reason || "").toLowerCase();
|
|
230
|
+
const r = (reason || "").trim().toLowerCase();
|
|
224
231
|
if (/auth service unavailable|temporarily unavailable/.test(r)) return "auth-unavailable";
|
|
225
|
-
if (
|
|
226
|
-
return "token-rejected";
|
|
227
|
-
}
|
|
232
|
+
if (r === "authentication failed") return "token-rejected";
|
|
228
233
|
return "generic";
|
|
229
234
|
}
|
|
230
235
|
|
package/src/ws-client.ts
CHANGED
|
@@ -270,6 +270,65 @@ export function isValidChatId(chatId: unknown): chatId is string {
|
|
|
270
270
|
*/
|
|
271
271
|
export type DeadChatSource = "signal" | "server";
|
|
272
272
|
|
|
273
|
+
/**
|
|
274
|
+
* §3.5 terminal `hello-fail` reason. Exact equality after trimming and
|
|
275
|
+
* lower-casing (parity with the hermes plugin's `_is_token_rejected`); never a
|
|
276
|
+
* substring or prefix match — see `onHelloFail`.
|
|
277
|
+
*/
|
|
278
|
+
export const TERMINAL_HELLO_FAIL_REASON = "authentication failed";
|
|
279
|
+
|
|
280
|
+
export function isTerminalHelloFailReason(reason: string | undefined): boolean {
|
|
281
|
+
return (reason ?? "").trim().toLowerCase() === TERMINAL_HELLO_FAIL_REASON;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* §3.6 duplicate-session refusal. msghub turns away the NEWER connection with
|
|
286
|
+
* this close code and a JSON reason `{reason, retry_after_ms}` when the opt-in
|
|
287
|
+
* guard is on. The server owns the 60s → 300s escalation, so the client only
|
|
288
|
+
* has to wait the value it is handed.
|
|
289
|
+
*/
|
|
290
|
+
export const CLOSE_CODE_DUPLICATE_THROTTLED = 4002;
|
|
291
|
+
/** Floor used when a 4002 close carries no parseable retry_after_ms. */
|
|
292
|
+
export const DEFAULT_REFUSAL_FLOOR_MS = 60_000;
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* §3.6 duplicate-session takeover. msghub closes the OLDER socket with 4001
|
|
296
|
+
* when a newer session for the same (user, device) takes over. Reconnecting at
|
|
297
|
+
* the ordinary initial delay races that session and kicks it back — the
|
|
298
|
+
* mutual-eviction storm.
|
|
299
|
+
*
|
|
300
|
+
* A single eviction waits the base floor; consecutive evictions of the same
|
|
301
|
+
* connection escalate it (doubling, capped), so whichever instance keeps losing
|
|
302
|
+
* withdraws further each round until the other simply stays connected. Prod
|
|
303
|
+
* measured 9–163 s per eviction (msghub 2026-08-18), longer than the base, so a
|
|
304
|
+
* flat floor cannot break a persistent loop — only the escalation does. The
|
|
305
|
+
* streak survives `hello-ok` (an eviction happens *after* we connect) and is
|
|
306
|
+
* reset only by a non-4001 close, mirroring the hermes plugin.
|
|
307
|
+
*/
|
|
308
|
+
export const CLOSE_CODE_REPLACED = 4001;
|
|
309
|
+
/** Base floor for the first takeover eviction (the §3.6 minimum). */
|
|
310
|
+
export const TAKEOVER_FLOOR_MS = 5_000;
|
|
311
|
+
/** Ceiling for the escalating takeover floor. */
|
|
312
|
+
export const TAKEOVER_FLOOR_MAX_MS = 600_000;
|
|
313
|
+
|
|
314
|
+
/** Reconnect floor for `streak` consecutive 4001 evictions (0 → no floor). */
|
|
315
|
+
export function takeoverFloorMs(streak: number): number {
|
|
316
|
+
if (streak <= 0) return 0;
|
|
317
|
+
return Math.min(TAKEOVER_FLOOR_MS * 2 ** (streak - 1), TAKEOVER_FLOOR_MAX_MS);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
export function parseRefusalRetryAfterMs(reason: string | undefined): number {
|
|
321
|
+
if (reason) {
|
|
322
|
+
try {
|
|
323
|
+
const value = (JSON.parse(reason) as { retry_after_ms?: unknown }).retry_after_ms;
|
|
324
|
+
if (typeof value === "number" && Number.isFinite(value) && value > 0) return value;
|
|
325
|
+
} catch {
|
|
326
|
+
// fall through to the default floor
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
return DEFAULT_REFUSAL_FLOOR_MS;
|
|
330
|
+
}
|
|
331
|
+
|
|
273
332
|
export class ClawChatClient extends EventEmitter {
|
|
274
333
|
private currentState: ConnState = "idle";
|
|
275
334
|
private connectResolve?: () => void;
|
|
@@ -278,6 +337,10 @@ export class ClawChatClient extends EventEmitter {
|
|
|
278
337
|
private pongTimer?: ReturnType<typeof setTimeout>;
|
|
279
338
|
private reconnectTimer?: ReturnType<typeof setTimeout>;
|
|
280
339
|
private reconnectAttempts = 0;
|
|
340
|
+
// Consecutive 4001 duplicate-session evictions. Unlike reconnectAttempts it
|
|
341
|
+
// is NOT reset on hello-ok (the eviction lands after we connect); a non-4001
|
|
342
|
+
// close resets it. Drives the escalating takeover floor (§3.6).
|
|
343
|
+
private takeoverStreak = 0;
|
|
281
344
|
private closing = false;
|
|
282
345
|
private authFailed = false;
|
|
283
346
|
private expectedConnectTraceId?: string;
|
|
@@ -747,13 +810,16 @@ export class ClawChatClient extends EventEmitter {
|
|
|
747
810
|
this.failHandshake(new ProtocolError("invalid hello-fail payload", env), 4002, "protocol error");
|
|
748
811
|
return;
|
|
749
812
|
}
|
|
750
|
-
// §
|
|
751
|
-
//
|
|
752
|
-
//
|
|
753
|
-
//
|
|
754
|
-
//
|
|
755
|
-
//
|
|
756
|
-
|
|
813
|
+
// §3.5: `"authentication failed"` — matched by EXACT equality, never a
|
|
814
|
+
// substring — is the one terminal reason (the token was rejected by upstream
|
|
815
|
+
// auth). Every other reason is transient: `remote auth service unavailable`
|
|
816
|
+
// (the auth backend is down, the token may still be valid — §14.1), `nonce
|
|
817
|
+
// mismatch`, `invalid connect …`, and any string msghub adds after this
|
|
818
|
+
// client shipped. For all of those: backoff-reconnect with the SAME token,
|
|
819
|
+
// keep the outbound queue, and do NOT refresh (a 5xx storm must not become
|
|
820
|
+
// a mass token-refresh storm; a new server-side reason must not brick old
|
|
821
|
+
// clients). Widening the terminal branch breaks that forward-compat rule.
|
|
822
|
+
if (!isTerminalHelloFailReason(reason)) {
|
|
757
823
|
const err = new TransportError(reason);
|
|
758
824
|
this.expectedConnectTraceId = undefined;
|
|
759
825
|
this.clearTimers();
|
|
@@ -763,8 +829,12 @@ export class ClawChatClient extends EventEmitter {
|
|
|
763
829
|
this.connectReject = undefined;
|
|
764
830
|
this.emitError(err);
|
|
765
831
|
if (this.opts.transport.state !== "closed") {
|
|
766
|
-
// Close WITHOUT marking closing/authFailed so handleClose
|
|
767
|
-
|
|
832
|
+
// Close WITHOUT marking closing/authFailed so handleClose
|
|
833
|
+
// backoff-reconnects. Use 1000, NOT 4001: 4001 is the server's
|
|
834
|
+
// duplicate-session takeover code that handleClose now floors to 5s
|
|
835
|
+
// (§3.6), and this client-initiated transient teardown must reconnect
|
|
836
|
+
// on the ordinary backoff instead of inheriting that floor.
|
|
837
|
+
this.opts.transport.close(1000, reason);
|
|
768
838
|
} else if (!this.closing && this.opts.reconnect.enabled) {
|
|
769
839
|
this.scheduleReconnect(reason);
|
|
770
840
|
} else {
|
|
@@ -875,7 +945,25 @@ export class ClawChatClient extends EventEmitter {
|
|
|
875
945
|
this.connectReject?.(closeError);
|
|
876
946
|
return;
|
|
877
947
|
}
|
|
878
|
-
|
|
948
|
+
// §3.6: raise an explicit reconnect floor for the two server-timed
|
|
949
|
+
// duplicate-session closes. 4002 (refusal) waits the server's
|
|
950
|
+
// retry_after_ms, which exceeds the maxDelay cap; 4001 (takeover) waits at
|
|
951
|
+
// least 5s so an instant reconnect doesn't re-enter the eviction storm.
|
|
952
|
+
let floorMs: number | undefined;
|
|
953
|
+
if (code === CLOSE_CODE_DUPLICATE_THROTTLED) {
|
|
954
|
+
// A refusal is not a takeover-loss — reset the streak and wait the
|
|
955
|
+
// server's retry_after_ms.
|
|
956
|
+
this.takeoverStreak = 0;
|
|
957
|
+
floorMs = parseRefusalRetryAfterMs(reason);
|
|
958
|
+
} else if (code === CLOSE_CODE_REPLACED) {
|
|
959
|
+
this.takeoverStreak += 1;
|
|
960
|
+
floorMs = takeoverFloorMs(this.takeoverStreak);
|
|
961
|
+
} else {
|
|
962
|
+
// Any other close (ordinary drop, backpressure kick) is not an eviction
|
|
963
|
+
// loss — reset so the next takeover starts from the base floor again.
|
|
964
|
+
this.takeoverStreak = 0;
|
|
965
|
+
}
|
|
966
|
+
this.scheduleReconnect(reason || `close ${code}`, floorMs);
|
|
879
967
|
}
|
|
880
968
|
|
|
881
969
|
private failHandshake(err: Error, code: number, reason: string): void {
|
|
@@ -894,7 +982,7 @@ export class ClawChatClient extends EventEmitter {
|
|
|
894
982
|
}
|
|
895
983
|
}
|
|
896
984
|
|
|
897
|
-
private scheduleReconnect(reason: string): void {
|
|
985
|
+
private scheduleReconnect(reason: string, floorMs?: number): void {
|
|
898
986
|
if (this.reconnectTimer) return;
|
|
899
987
|
if (this.reconnectAttempts >= this.opts.reconnect.maxRetries) {
|
|
900
988
|
this.sendQueue.length = 0;
|
|
@@ -903,10 +991,13 @@ export class ClawChatClient extends EventEmitter {
|
|
|
903
991
|
return;
|
|
904
992
|
}
|
|
905
993
|
this.reconnectAttempts += 1;
|
|
906
|
-
const
|
|
994
|
+
const cappedDelay = Math.min(
|
|
907
995
|
this.opts.reconnect.maxDelay,
|
|
908
996
|
this.opts.reconnect.initialDelay * 2 ** Math.max(0, this.reconnectAttempts - 1),
|
|
909
997
|
);
|
|
998
|
+
// An explicit floor (a 4002 retry_after_ms) overrides the maxDelay cap: the
|
|
999
|
+
// server's wait is deliberately longer than the ordinary backoff ceiling.
|
|
1000
|
+
const baseDelay = floorMs !== undefined ? Math.max(cappedDelay, floorMs) : cappedDelay;
|
|
910
1001
|
const jitter = baseDelay * this.opts.reconnect.jitterRatio * Math.random();
|
|
911
1002
|
const delay = Math.round(baseDelay + jitter);
|
|
912
1003
|
this.transition("reconnecting");
|