@agentchatme/openclaw 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.
@@ -131,7 +131,7 @@ function classifyNetworkError(err) {
131
131
  }
132
132
 
133
133
  // src/version.ts
134
- var PACKAGE_VERSION = "0.8.0";
134
+ var PACKAGE_VERSION = "0.8.1";
135
135
 
136
136
  // src/client-identity.ts
137
137
  var AGENTCHAT_CLIENT_NAME = "openclaw";
@@ -1754,7 +1754,6 @@ function transition(state, event) {
1754
1754
  return { kind: "CONNECTING", attempt: event.attempt, startedAt: event.now };
1755
1755
  }
1756
1756
  if (event.type === "DRAIN_REQUESTED") return { kind: "CLOSED" };
1757
- if (event.type === "AUTH_REJECTED") return { kind: "AUTH_FAIL", reason: event.reason };
1758
1757
  return state;
1759
1758
  }
1760
1759
  case "CONNECTING": {
@@ -1800,6 +1799,9 @@ function transition(state, event) {
1800
1799
  return state;
1801
1800
  }
1802
1801
  case "AUTH_FAIL": {
1802
+ if (event.type === "CONNECT") {
1803
+ return { kind: "CONNECTING", attempt: event.attempt, startedAt: event.now };
1804
+ }
1803
1805
  if (event.type === "RECONFIGURED") return { kind: "DISCONNECTED" };
1804
1806
  if (event.type === "DRAIN_REQUESTED") return { kind: "CLOSED" };
1805
1807
  return state;
@@ -1810,15 +1812,25 @@ function transition(state, event) {
1810
1812
  }
1811
1813
  }
1812
1814
  function isTerminal(state) {
1813
- return state.kind === "CLOSED" || state.kind === "AUTH_FAIL";
1815
+ return state.kind === "CLOSED";
1814
1816
  }
1815
1817
 
1816
1818
  // src/ws-client.ts
1817
1819
  var HELLO_ACK_TIMEOUT_MS = 4e3;
1818
- var RECONNECT_HARD_CAP_ATTEMPTS = 60;
1820
+ var RECONNECT_LONG_OUTAGE_AFTER_ATTEMPTS = 60;
1821
+ var RECONNECT_LONG_OUTAGE_BACKOFF_MS = 5 * 6e4;
1822
+ var AUTH_RETRY_INTERVAL_MS = 75e4;
1819
1823
  var MAX_FRAME_BYTES = 2 * 1024 * 1024;
1820
1824
  var HELLO_CAPABILITIES = ["ack"];
1821
- var AUTH_CLOSE_CODES = /* @__PURE__ */ new Set([1008, 4401, 4403]);
1825
+ var UNAMBIGUOUS_AUTH_CLOSE_CODES = /* @__PURE__ */ new Set([4401, 4403]);
1826
+ var AMBIGUOUS_AUTH_CLOSE_CODE = 1008;
1827
+ var SERVER_AUTH_CLOSE_REASONS = ["invalid token"];
1828
+ function isHandshakeRejection(code, reason) {
1829
+ if (UNAMBIGUOUS_AUTH_CLOSE_CODES.has(code)) return true;
1830
+ if (code !== AMBIGUOUS_AUTH_CLOSE_CODE) return false;
1831
+ const lower = reason.toLowerCase();
1832
+ return SERVER_AUTH_CLOSE_REASONS.some((known) => lower.includes(known));
1833
+ }
1822
1834
  var AgentchatWsClient = class {
1823
1835
  config;
1824
1836
  logger;
@@ -1834,6 +1846,15 @@ var AgentchatWsClient = class {
1834
1846
  ws = null;
1835
1847
  attempt = 0;
1836
1848
  _ackNegotiated = false;
1849
+ /**
1850
+ * Set by `closeSocket()` before it closes the current socket, read and
1851
+ * cleared by `handleClose()`. A close this client asked for says nothing
1852
+ * about the server, whatever code travelled with it.
1853
+ */
1854
+ closeInitiatedLocally = false;
1855
+ /** One error line per long outage / per run of rejected keys, then debug. */
1856
+ longOutageAnnounced = false;
1857
+ authFailAnnounced = false;
1837
1858
  helloAckTimer = null;
1838
1859
  pingTimer = null;
1839
1860
  pongTimer = null;
@@ -1873,21 +1894,14 @@ var AgentchatWsClient = class {
1873
1894
  return this._ackNegotiated;
1874
1895
  }
1875
1896
  /**
1876
- * Kick off connection. No-op if already connecting, authenticating, or
1877
- * ready. Throws on `AUTH_FAIL` — the caller must call `reconfigured()`
1878
- * after rotating the API key before retrying.
1897
+ * Kick off connection. No-op if already connecting, authenticating, ready,
1898
+ * or waiting out a backoff / auth retry. Throws only on `CLOSED`.
1879
1899
  */
1880
1900
  start() {
1881
- if (this.state.kind === "AUTH_FAIL") {
1882
- throw new AgentChatChannelError(
1883
- "terminal-auth",
1884
- "cannot start: channel is in AUTH_FAIL \u2014 rotate api key and call reconfigured()"
1885
- );
1886
- }
1887
1901
  if (this.state.kind === "CLOSED") {
1888
1902
  throw new Error("cannot start: client is closed. Create a new instance.");
1889
1903
  }
1890
- if (this.state.kind !== "DISCONNECTED") {
1904
+ if (this.state.kind !== "DISCONNECTED" || this.reconnectTimer) {
1891
1905
  return;
1892
1906
  }
1893
1907
  this.openSocket(1);
@@ -1920,13 +1934,15 @@ var AgentchatWsClient = class {
1920
1934
  }
1921
1935
  }
1922
1936
  /**
1923
- * Signal that operator has rotated the API key. Transitions from
1924
- * AUTH_FAIL back to DISCONNECTED; the next `start()` call will attempt
1925
- * a fresh HELLO with the current config.
1937
+ * Signal that the operator has rotated the API key: leave AUTH_FAIL and
1938
+ * retry now instead of waiting for the slow retry timer. A no-op in every
1939
+ * other state (the client is already connecting or connected).
1926
1940
  */
1927
1941
  reconfigured() {
1928
1942
  if (this.state.kind !== "AUTH_FAIL") return;
1943
+ this.clearReconnectTimer();
1929
1944
  this.applyEvent({ type: "RECONFIGURED" });
1945
+ this.openSocket(1);
1930
1946
  }
1931
1947
  on(event, handler) {
1932
1948
  this.listeners[event].add(handler);
@@ -1936,7 +1952,9 @@ var AgentchatWsClient = class {
1936
1952
  }
1937
1953
  // ─── Internals: socket lifecycle ─────────────────────────────────────
1938
1954
  openSocket(attempt) {
1955
+ if (this.state.kind === "CLOSED") return;
1939
1956
  this.attempt = attempt;
1957
+ this.closeInitiatedLocally = false;
1940
1958
  const now = this.now();
1941
1959
  this.applyEvent({ type: "CONNECT", now, attempt });
1942
1960
  const url = `${this.config.apiBase.replace(/^http/, "ws")}/v1/ws`;
@@ -2008,7 +2026,7 @@ var AgentchatWsClient = class {
2008
2026
  this.emitError(
2009
2027
  new AgentChatChannelError("retry-transient", "hello ack timeout")
2010
2028
  );
2011
- this.closeSocket(1008, "hello ack timeout");
2029
+ this.closeSocket(1001, "hello ack timeout");
2012
2030
  }, HELLO_ACK_TIMEOUT_MS);
2013
2031
  }
2014
2032
  handleMessage(data, isBinary) {
@@ -2095,26 +2113,43 @@ var AgentchatWsClient = class {
2095
2113
  this.logger.info({ ackNegotiated: this._ackNegotiated }, "hello.ok");
2096
2114
  this.applyEvent({ type: "HELLO_OK", now });
2097
2115
  this.attempt = 0;
2116
+ this.longOutageAnnounced = false;
2117
+ this.authFailAnnounced = false;
2098
2118
  this.startHeartbeat();
2099
2119
  this.emit("authenticated", now);
2100
2120
  }
2121
+ /** The server answered HELLO with a rejection frame (not a close). */
2101
2122
  handleAuthRejected(reason) {
2102
2123
  if (this.helloAckTimer) {
2103
2124
  this._clearTimeout(this.helloAckTimer);
2104
2125
  this.helloAckTimer = null;
2105
2126
  }
2106
- this.logger.error({ reason }, "auth rejected \u2014 moving to AUTH_FAIL");
2127
+ this.enterAuthFail(reason);
2128
+ this.closeSocket(1e3, "auth rejected");
2129
+ }
2130
+ /**
2131
+ * The server rejected this key. AUTH_FAIL is not the end: retry a fresh
2132
+ * HELLO every `AUTH_RETRY_INTERVAL_MS` (jittered). The first rejection in a
2133
+ * run logs at error so an operator sees it; the following ones at debug.
2134
+ */
2135
+ enterAuthFail(reason) {
2136
+ this.logger[this.authFailAnnounced ? "debug" : "error"](
2137
+ { reason, retryIntervalMs: AUTH_RETRY_INTERVAL_MS, attempt: this.attempt },
2138
+ "auth rejected \u2014 retrying slowly until the key is accepted again"
2139
+ );
2140
+ this.authFailAnnounced = true;
2107
2141
  this.applyEvent({ type: "AUTH_REJECTED", reason });
2108
2142
  this.emitError(
2109
2143
  new AgentChatChannelError("terminal-auth", `auth rejected: ${reason}`)
2110
2144
  );
2111
- this.closeSocket(1008, "auth rejected");
2145
+ this.scheduleAuthRetry();
2112
2146
  }
2113
2147
  handleClose(code, reason) {
2114
- this.now();
2115
2148
  const wasAuthenticating = this.state.kind === "AUTHENTICATING";
2116
2149
  const wasDraining = this.state.kind === "DRAINING";
2117
- const authClass = AUTH_CLOSE_CODES.has(code);
2150
+ const ours = this.closeInitiatedLocally;
2151
+ this.closeInitiatedLocally = false;
2152
+ const serverAuthClose = !ours && isHandshakeRejection(code, reason);
2118
2153
  this.stopHeartbeat();
2119
2154
  if (this.helloAckTimer) {
2120
2155
  this._clearTimeout(this.helloAckTimer);
@@ -2122,19 +2157,17 @@ var AgentchatWsClient = class {
2122
2157
  }
2123
2158
  this.ws = null;
2124
2159
  this._ackNegotiated = false;
2125
- if (this.state.kind === "CLOSED") {
2160
+ const kind = this.state.kind;
2161
+ if (kind === "CLOSED") {
2126
2162
  this.logger.debug({ code, reason }, "socket closed after CLOSED \u2014 no-op");
2127
2163
  return;
2128
2164
  }
2129
- if (authClass && wasAuthenticating) {
2130
- this.applyEvent({ type: "AUTH_REJECTED", reason: `close ${code}: ${reason}` });
2131
- this.emitError(
2132
- new AgentChatChannelError(
2133
- "terminal-auth",
2134
- `socket closed with auth code ${code}: ${reason}`
2135
- )
2136
- );
2137
- this.emit("closed");
2165
+ if (kind === "AUTH_FAIL") {
2166
+ this.logger.debug({ code, reason }, "socket closed after AUTH_FAIL \u2014 retry already scheduled");
2167
+ return;
2168
+ }
2169
+ if (serverAuthClose && wasAuthenticating) {
2170
+ this.enterAuthFail(`close ${code}: ${reason}`);
2138
2171
  return;
2139
2172
  }
2140
2173
  this.applyEvent({ type: "SOCKET_CLOSED", code, reason });
@@ -2143,13 +2176,14 @@ var AgentchatWsClient = class {
2143
2176
  return;
2144
2177
  }
2145
2178
  this.logger.info(
2146
- { code, reason, attempt: this.attempt },
2179
+ { code, reason, ours, attempt: this.attempt },
2147
2180
  "socket closed \u2014 scheduling reconnect"
2148
2181
  );
2149
2182
  this.scheduleReconnect(`close-${code}`);
2150
2183
  }
2151
2184
  closeSocket(code, reason) {
2152
2185
  if (!this.ws) return;
2186
+ this.closeInitiatedLocally = true;
2153
2187
  try {
2154
2188
  this.ws.close(code, reason);
2155
2189
  } catch {
@@ -2209,14 +2243,15 @@ var AgentchatWsClient = class {
2209
2243
  if (this.state.kind === "CLOSED" || this.state.kind === "AUTH_FAIL") return;
2210
2244
  if (this.reconnectTimer) return;
2211
2245
  const nextAttempt = this.attempt + 1;
2212
- if (nextAttempt > RECONNECT_HARD_CAP_ATTEMPTS) {
2213
- const msg = `reconnect hard cap (${RECONNECT_HARD_CAP_ATTEMPTS}) reached`;
2214
- this.logger.error({ attempt: nextAttempt }, msg);
2215
- this.applyEvent({ type: "AUTH_REJECTED", reason: msg });
2216
- this.emitError(
2217
- new AgentChatChannelError("terminal-auth", msg)
2246
+ if (nextAttempt > RECONNECT_LONG_OUTAGE_AFTER_ATTEMPTS && !this.longOutageAnnounced) {
2247
+ this.longOutageAnnounced = true;
2248
+ this.logger.error(
2249
+ {
2250
+ attempt: nextAttempt,
2251
+ maxBackoffMs: Math.max(this.config.reconnect.maxBackoffMs, RECONNECT_LONG_OUTAGE_BACKOFF_MS)
2252
+ },
2253
+ `still reconnecting after ${RECONNECT_LONG_OUTAGE_AFTER_ATTEMPTS} attempts \u2014 backing off further, not giving up`
2218
2254
  );
2219
- return;
2220
2255
  }
2221
2256
  const delay = this.computeReconnectDelay(nextAttempt);
2222
2257
  this.metrics.incReconnect({ reason });
@@ -2229,13 +2264,41 @@ var AgentchatWsClient = class {
2229
2264
  this.openSocket(nextAttempt);
2230
2265
  }, delay);
2231
2266
  }
2267
+ /** AUTH_FAIL's own cadence: a fresh HELLO every AUTH_RETRY_INTERVAL_MS, jittered. */
2268
+ scheduleAuthRetry() {
2269
+ if (this.state.kind !== "AUTH_FAIL") return;
2270
+ this.clearReconnectTimer();
2271
+ const nextAttempt = this.attempt + 1;
2272
+ const delay = this.jittered(AUTH_RETRY_INTERVAL_MS);
2273
+ this.metrics.incReconnect({ reason: "auth-retry" });
2274
+ this.logger.debug({ attempt: nextAttempt, delayMs: delay }, "scheduling auth retry");
2275
+ this.reconnectTimer = this._setTimeout(() => {
2276
+ this.reconnectTimer = null;
2277
+ this.openSocket(nextAttempt);
2278
+ }, delay);
2279
+ }
2280
+ clearReconnectTimer() {
2281
+ if (this.reconnectTimer) {
2282
+ this._clearTimeout(this.reconnectTimer);
2283
+ this.reconnectTimer = null;
2284
+ }
2285
+ }
2232
2286
  computeReconnectDelay(attempt) {
2233
- const { initialBackoffMs, maxBackoffMs, jitterRatio } = this.config.reconnect;
2287
+ const { initialBackoffMs, maxBackoffMs } = this.config.reconnect;
2234
2288
  const reconnectCount = Math.max(1, attempt - 1);
2235
2289
  const exp = initialBackoffMs * Math.pow(2, Math.min(reconnectCount - 1, 20));
2236
- const capped = Math.min(exp, maxBackoffMs);
2290
+ const cap = attempt > RECONNECT_LONG_OUTAGE_AFTER_ATTEMPTS ? Math.max(maxBackoffMs, RECONNECT_LONG_OUTAGE_BACKOFF_MS) : maxBackoffMs;
2291
+ return this.jittered(Math.min(exp, cap));
2292
+ }
2293
+ /**
2294
+ * Symmetric ±`jitterRatio`. Thundering-herd-safe: a fleet that all drops
2295
+ * at the same instant re-enters the connect queue spread across
2296
+ * ms * [1-jitterRatio, 1+jitterRatio].
2297
+ */
2298
+ jittered(ms) {
2299
+ const { jitterRatio } = this.config.reconnect;
2237
2300
  const jitter = 1 - jitterRatio + this.random() * 2 * jitterRatio;
2238
- return Math.max(0, Math.floor(capped * jitter));
2301
+ return Math.max(0, Math.floor(ms * jitter));
2239
2302
  }
2240
2303
  // ─── Internals: drain ────────────────────────────────────────────────
2241
2304
  scheduleDrainDeadline(deadline) {
@@ -2278,6 +2341,9 @@ var AgentchatWsClient = class {
2278
2341
  return;
2279
2342
  }
2280
2343
  this.state = next;
2344
+ if (next.kind === "CLOSED") {
2345
+ this.clearReconnectTimer();
2346
+ }
2281
2347
  this.metrics.setConnectionState(next.kind);
2282
2348
  this.logger.info(
2283
2349
  { from: prev.kind, to: next.kind, event: event.type },
@@ -3388,6 +3454,7 @@ var agentchatGatewayAdapter = {
3388
3454
  gateFailOpen: readReplyGateFailOpenFromEnv(),
3389
3455
  ...gateTimeoutMs !== void 0 ? { gateTimeoutMs } : {}
3390
3456
  });
3457
+ let authBlocked = false;
3391
3458
  return {
3392
3459
  invoker,
3393
3460
  handlers: {
@@ -3399,16 +3466,39 @@ var agentchatGatewayAdapter = {
3399
3466
  void catchUpInvites(ctx.accountId, config, invoker, logger);
3400
3467
  },
3401
3468
  onError: (err) => {
3402
- ctx.log?.warn?.(`[agentchat:${ctx.accountId}] runtime error: ${err.message} (class=${err.class_})`);
3469
+ const level = err.class_ === "terminal-auth" && authBlocked ? "debug" : "warn";
3470
+ ctx.log?.[level]?.(`[agentchat:${ctx.accountId}] runtime error: ${err.message} (class=${err.class_})`);
3403
3471
  },
3404
3472
  onStateChanged: (next) => {
3405
- const running = next.kind !== "CLOSED" && next.kind !== "AUTH_FAIL";
3406
- const connected = next.kind === "READY" || next.kind === "DEGRADED";
3407
- const lifecycle = next.kind === "READY" || next.kind === "DEGRADED" ? "ready" : next.kind === "AUTH_FAIL" ? "blocked" : next.kind === "CLOSED" ? "stopped" : runtime.getHealth().counters.knocks > 0 ? "recovering" : "starting";
3408
- ctx.setStatus(withLifecycle({ ...ctx.getStatus(), running, connected, statusState: statusStateFor(next.kind) }, lifecycle));
3409
3473
  if (next.kind === "AUTH_FAIL") {
3410
- ctx.log?.error?.(`[agentchat:${ctx.accountId}] AUTH_FAIL \u2014 rotate the key or re-run openclaw channels add agentchat`);
3474
+ if (authBlocked) {
3475
+ ctx.log?.debug?.(`[agentchat:${ctx.accountId}] still AUTH_FAIL (${next.reason}) \u2014 next retry in 10\u201315 min`);
3476
+ return;
3477
+ }
3478
+ authBlocked = true;
3479
+ ctx.log?.error?.(
3480
+ `[agentchat:${ctx.accountId}] AUTH_FAIL (${next.reason}) \u2014 the server rejected the key; retrying every 10\u201315 min; rotate the key or re-run openclaw channels add agentchat`
3481
+ );
3482
+ ctx.setStatus(
3483
+ withLifecycle(
3484
+ { ...ctx.getStatus(), running: false, connected: false, statusState: "not-linked", lastDisconnect: { at: Date.now(), error: `AUTH_FAIL: ${next.reason}` } },
3485
+ "blocked"
3486
+ )
3487
+ );
3488
+ return;
3489
+ }
3490
+ if (authBlocked && next.kind !== "READY" && next.kind !== "CLOSED") {
3491
+ ctx.log?.debug?.(`[agentchat:${ctx.accountId}] AUTH_FAIL retry: ${next.kind.toLowerCase()}`);
3492
+ return;
3493
+ }
3494
+ if (authBlocked) {
3495
+ authBlocked = false;
3496
+ if (next.kind === "READY") ctx.log?.info?.(`[agentchat:${ctx.accountId}] key accepted again \u2014 AUTH_FAIL cleared`);
3411
3497
  }
3498
+ const running = next.kind !== "CLOSED";
3499
+ const connected = next.kind === "READY" || next.kind === "DEGRADED";
3500
+ const lifecycle = next.kind === "READY" || next.kind === "DEGRADED" ? "ready" : next.kind === "CLOSED" ? "stopped" : runtime.getHealth().counters.knocks > 0 ? "recovering" : "starting";
3501
+ ctx.setStatus(withLifecycle({ ...ctx.getStatus(), running, connected, statusState: statusStateFor(next.kind) }, lifecycle));
3412
3502
  }
3413
3503
  }
3414
3504
  };