rivetkit 0.0.0-sqlite-profiling-compat.4a9a623 → 0.0.0-sqlite-sync-3-0-0-release.14341d0

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 (277) hide show
  1. package/dist/browser/client.d.ts +1317 -765
  2. package/dist/browser/client.js +1433 -692
  3. package/dist/browser/client.js.map +1 -1
  4. package/dist/browser/inspector/client.d.ts +252 -129
  5. package/dist/browser/inspector/client.js +2042 -228
  6. package/dist/browser/inspector/client.js.map +1 -1
  7. package/dist/tsup/actor/errors.cjs +6 -2
  8. package/dist/tsup/actor/errors.cjs.map +1 -1
  9. package/dist/tsup/actor/errors.d.cts +1 -1
  10. package/dist/tsup/actor/errors.d.ts +1 -1
  11. package/dist/tsup/actor/errors.js +5 -1
  12. package/dist/tsup/agent-os/index.cjs +3 -2367
  13. package/dist/tsup/agent-os/index.cjs.map +1 -1
  14. package/dist/tsup/agent-os/index.d.cts +1 -2277
  15. package/dist/tsup/agent-os/index.d.ts +1 -2277
  16. package/dist/tsup/agent-os/index.js +3 -2367
  17. package/dist/tsup/agent-os/index.js.map +1 -1
  18. package/dist/tsup/{chunk-AGDY6TKH.js → chunk-2EQ2WCO7.js} +1054 -364
  19. package/dist/tsup/chunk-2EQ2WCO7.js.map +1 -0
  20. package/dist/tsup/{chunk-7QKCGUTJ.cjs → chunk-4H2LT2WB.cjs} +444 -29
  21. package/dist/tsup/chunk-4H2LT2WB.cjs.map +1 -0
  22. package/dist/tsup/{chunk-XIX5DOZN.js → chunk-6FEYCVZC.js} +196 -20
  23. package/dist/tsup/chunk-6FEYCVZC.js.map +1 -0
  24. package/dist/tsup/{chunk-QAVN4QNO.cjs → chunk-6WYQ3NJI.cjs} +7 -7
  25. package/dist/tsup/chunk-6WYQ3NJI.cjs.map +1 -0
  26. package/dist/tsup/{chunk-75UE45DE.js → chunk-6ZJX3KXW.js} +436 -21
  27. package/dist/tsup/chunk-6ZJX3KXW.js.map +1 -0
  28. package/dist/tsup/{chunk-HXUEHHJF.js → chunk-A2CF6PFH.js} +19 -11
  29. package/dist/tsup/chunk-A2CF6PFH.js.map +1 -0
  30. package/dist/tsup/{chunk-RIT7N3TX.js → chunk-AIGW2SKA.js} +50 -3
  31. package/dist/tsup/chunk-AIGW2SKA.js.map +1 -0
  32. package/dist/tsup/{chunk-3MHDOUD7.cjs → chunk-HDVZ6YZG.cjs} +197 -21
  33. package/dist/tsup/chunk-HDVZ6YZG.cjs.map +1 -0
  34. package/dist/tsup/{chunk-2CICCOBL.cjs → chunk-HU4F7H3F.cjs} +52 -5
  35. package/dist/tsup/chunk-HU4F7H3F.cjs.map +1 -0
  36. package/dist/tsup/{chunk-4PX3ZKJR.js → chunk-JNJEWQZL.js} +3 -3
  37. package/dist/tsup/chunk-JNJEWQZL.js.map +1 -0
  38. package/dist/tsup/{chunk-JZ7TWV65.js → chunk-JTHHCZCZ.js} +24 -1
  39. package/dist/tsup/chunk-JTHHCZCZ.js.map +1 -0
  40. package/dist/tsup/{chunk-ZA7FLHKH.cjs → chunk-KVFIONKQ.cjs} +25 -2
  41. package/dist/tsup/chunk-KVFIONKQ.cjs.map +1 -0
  42. package/dist/tsup/{chunk-4FC7TVS6.cjs → chunk-MGI2VVF6.cjs} +39 -7
  43. package/dist/tsup/chunk-MGI2VVF6.cjs.map +1 -0
  44. package/dist/tsup/{chunk-NHUTYWKU.cjs → chunk-NE5CUQRN.cjs} +1173 -483
  45. package/dist/tsup/chunk-NE5CUQRN.cjs.map +1 -0
  46. package/dist/tsup/{chunk-IICIOTML.js → chunk-OX5MX7TO.js} +2 -2
  47. package/dist/tsup/{chunk-ND567FR5.cjs → chunk-PSIBTOAR.cjs} +3 -3
  48. package/dist/tsup/chunk-PSIBTOAR.cjs.map +1 -0
  49. package/dist/tsup/chunk-PW6FYEIV.cjs +14 -0
  50. package/dist/tsup/chunk-PW6FYEIV.cjs.map +1 -0
  51. package/dist/tsup/{chunk-VE2X4KMG.cjs → chunk-RTWH3AX3.cjs} +18 -10
  52. package/dist/tsup/chunk-RTWH3AX3.cjs.map +1 -0
  53. package/dist/tsup/chunk-SLICBKAU.js +14 -0
  54. package/dist/tsup/chunk-SLICBKAU.js.map +1 -0
  55. package/dist/tsup/{chunk-XFYEVYHG.js → chunk-WCVYMFWJ.js} +16 -9
  56. package/dist/tsup/chunk-WCVYMFWJ.js.map +1 -0
  57. package/dist/tsup/chunk-WX3JS4XU.cjs +243 -0
  58. package/dist/tsup/chunk-WX3JS4XU.cjs.map +1 -0
  59. package/dist/tsup/chunk-X32L7FDD.js +243 -0
  60. package/dist/tsup/chunk-X32L7FDD.js.map +1 -0
  61. package/dist/tsup/{chunk-2WMYYG4R.cjs → chunk-XAY2HIZN.cjs} +18 -11
  62. package/dist/tsup/chunk-XAY2HIZN.cjs.map +1 -0
  63. package/dist/tsup/{chunk-ZZ3WBRPD.js → chunk-ZOVJDAOW.js} +38 -6
  64. package/dist/tsup/chunk-ZOVJDAOW.js.map +1 -0
  65. package/dist/tsup/client/mod.cjs +8 -9
  66. package/dist/tsup/client/mod.cjs.map +1 -1
  67. package/dist/tsup/client/mod.d.cts +32 -6
  68. package/dist/tsup/client/mod.d.ts +32 -6
  69. package/dist/tsup/client/mod.js +7 -8
  70. package/dist/tsup/common/log.cjs +5 -3
  71. package/dist/tsup/common/log.cjs.map +1 -1
  72. package/dist/tsup/common/log.d.cts +5 -1
  73. package/dist/tsup/common/log.d.ts +5 -1
  74. package/dist/tsup/common/log.js +6 -4
  75. package/dist/tsup/common/websocket.cjs +4 -4
  76. package/dist/tsup/common/websocket.cjs.map +1 -1
  77. package/dist/tsup/common/websocket.js +3 -3
  78. package/dist/tsup/{config-BR1Nn0QQ.d.ts → config-B4erdavw.d.ts} +571 -65
  79. package/dist/tsup/{config-B0LNDrLY.d.ts → config-BfG1qpYz.d.cts} +80 -14
  80. package/dist/tsup/{config-B0LNDrLY.d.cts → config-BfG1qpYz.d.ts} +80 -14
  81. package/dist/tsup/{config-tCvkwzyN.d.cts → config-DK144OAw.d.cts} +571 -65
  82. package/dist/tsup/{context-oc9zhA3x.d.ts → context-Cvnf3BRe.d.cts} +3 -2
  83. package/dist/tsup/{context-D4WDwf9m.d.cts → context-Oxi6YWcG.d.ts} +3 -2
  84. package/dist/tsup/db/drizzle.cjs +127 -67
  85. package/dist/tsup/db/drizzle.cjs.map +1 -1
  86. package/dist/tsup/db/drizzle.d.cts +8 -4
  87. package/dist/tsup/db/drizzle.d.ts +8 -4
  88. package/dist/tsup/db/drizzle.js +127 -67
  89. package/dist/tsup/db/drizzle.js.map +1 -1
  90. package/dist/tsup/db/mod.cjs +6 -126
  91. package/dist/tsup/db/mod.cjs.map +1 -1
  92. package/dist/tsup/db/mod.d.cts +8 -4
  93. package/dist/tsup/db/mod.d.ts +8 -4
  94. package/dist/tsup/db/mod.js +5 -125
  95. package/dist/tsup/db/mod.js.map +1 -1
  96. package/dist/tsup/dynamic/mod.cjs +2 -2
  97. package/dist/tsup/dynamic/mod.cjs.map +1 -1
  98. package/dist/tsup/dynamic/mod.d.cts +6 -4
  99. package/dist/tsup/dynamic/mod.d.ts +6 -4
  100. package/dist/tsup/dynamic/mod.js +1 -1
  101. package/dist/tsup/inspector/mod.cjs +47 -12
  102. package/dist/tsup/inspector/mod.cjs.map +1 -1
  103. package/dist/tsup/inspector/mod.d.cts +26 -136
  104. package/dist/tsup/inspector/mod.d.ts +26 -136
  105. package/dist/tsup/inspector/mod.js +46 -11
  106. package/dist/tsup/inspector/mod.js.map +1 -1
  107. package/dist/tsup/inspector/workflow.cjs +21 -0
  108. package/dist/tsup/inspector/workflow.cjs.map +1 -0
  109. package/dist/tsup/inspector/workflow.d.cts +144 -0
  110. package/dist/tsup/inspector/workflow.d.ts +144 -0
  111. package/dist/tsup/inspector/workflow.js +21 -0
  112. package/dist/tsup/inspector/workflow.js.map +1 -0
  113. package/dist/tsup/inspector-tab/mod.cjs +2 -13
  114. package/dist/tsup/inspector-tab/mod.cjs.map +1 -1
  115. package/dist/tsup/inspector-tab/mod.d.cts +9 -17
  116. package/dist/tsup/inspector-tab/mod.d.ts +9 -17
  117. package/dist/tsup/inspector-tab/mod.js +1 -12
  118. package/dist/tsup/inspector-tab/mod.js.map +1 -1
  119. package/dist/tsup/mod.cjs +2728 -611
  120. package/dist/tsup/mod.cjs.map +1 -1
  121. package/dist/tsup/mod.d.cts +9 -7
  122. package/dist/tsup/mod.d.ts +9 -7
  123. package/dist/tsup/mod.js +2645 -528
  124. package/dist/tsup/mod.js.map +1 -1
  125. package/dist/tsup/test/mod.cjs +19 -17
  126. package/dist/tsup/test/mod.cjs.map +1 -1
  127. package/dist/tsup/test/mod.d.cts +6 -4
  128. package/dist/tsup/test/mod.d.ts +6 -4
  129. package/dist/tsup/test/mod.js +18 -16
  130. package/dist/tsup/test/mod.js.map +1 -1
  131. package/dist/tsup/unstable/migrations.cjs +101 -0
  132. package/dist/tsup/unstable/migrations.cjs.map +1 -0
  133. package/dist/tsup/unstable/migrations.d.cts +29 -0
  134. package/dist/tsup/unstable/migrations.d.ts +29 -0
  135. package/dist/tsup/unstable/migrations.js +101 -0
  136. package/dist/tsup/unstable/migrations.js.map +1 -0
  137. package/dist/tsup/{utils-CqDnC_PS.d.cts → utils-3rsE0JvI.d.cts} +25 -2
  138. package/dist/tsup/{utils-CqDnC_PS.d.ts → utils-3rsE0JvI.d.ts} +25 -2
  139. package/dist/tsup/utils-C1O-SklO.d.ts +162 -0
  140. package/dist/tsup/utils-C6b0aYUM.d.cts +162 -0
  141. package/dist/tsup/utils.cjs +7 -3
  142. package/dist/tsup/utils.cjs.map +1 -1
  143. package/dist/tsup/utils.d.cts +4 -147
  144. package/dist/tsup/utils.d.ts +4 -147
  145. package/dist/tsup/utils.js +6 -2
  146. package/dist/tsup/workflow/mod.cjs +260 -200
  147. package/dist/tsup/workflow/mod.cjs.map +1 -1
  148. package/dist/tsup/workflow/mod.d.cts +8 -6
  149. package/dist/tsup/workflow/mod.d.ts +8 -6
  150. package/dist/tsup/workflow/mod.js +249 -189
  151. package/dist/tsup/workflow/mod.js.map +1 -1
  152. package/package.json +34 -14
  153. package/src/actor/actions.ts +155 -0
  154. package/src/actor/config.ts +403 -43
  155. package/src/actor/definition.ts +26 -7
  156. package/src/actor/errors.ts +87 -5
  157. package/src/actor/keys.ts +86 -0
  158. package/src/actor/mod.ts +15 -1
  159. package/src/actor/tracing.ts +28 -0
  160. package/src/agent-os/index.ts +5 -65
  161. package/src/client/actor-common.ts +8 -15
  162. package/src/client/actor-conn.ts +178 -25
  163. package/src/client/actor-handle.ts +180 -42
  164. package/src/client/actor-query.ts +8 -1
  165. package/src/client/auth.ts +37 -0
  166. package/src/client/client.ts +89 -21
  167. package/src/client/config.ts +27 -2
  168. package/src/client/errors.ts +2 -1
  169. package/src/client/lifecycle-errors.ts +29 -1
  170. package/src/client/mod.ts +9 -0
  171. package/src/client/outbound-telemetry.ts +66 -0
  172. package/src/client/query.ts +2 -0
  173. package/src/client/queue.ts +3 -0
  174. package/src/client/raw-utils.ts +37 -8
  175. package/src/client/resolve-gateway-target.ts +2 -0
  176. package/src/client/retry-budget.ts +78 -0
  177. package/src/client/token-provider.ts +93 -0
  178. package/src/client/utils.ts +16 -2
  179. package/src/common/actor-router-consts.ts +5 -0
  180. package/src/common/actor-telemetry-context.ts +19 -0
  181. package/src/common/bare/generated/actor-runtime-socket-protocol/v1.ts +848 -0
  182. package/src/common/bare/generated/inspector/v5.ts +1016 -0
  183. package/src/common/bare/generated/inspector/v6.ts +1367 -0
  184. package/src/common/bare/transport/v1.ts +36 -0
  185. package/src/common/database/config.ts +108 -15
  186. package/src/common/database/mod.ts +247 -76
  187. package/src/common/database/native-database.ts +433 -23
  188. package/src/common/database/shared.ts +24 -0
  189. package/src/common/encoding.ts +31 -21
  190. package/src/common/fetch-like.ts +63 -0
  191. package/src/common/inspector-transport.ts +12 -2
  192. package/src/common/log.ts +14 -8
  193. package/src/common/otel-context.ts +99 -0
  194. package/src/common/router.ts +1 -1
  195. package/src/common/utils.ts +7 -19
  196. package/src/db/drizzle.ts +182 -80
  197. package/src/db/mod.ts +2 -0
  198. package/src/dynamic/isolate-runtime.ts +0 -2
  199. package/src/engine-api/actors.ts +3 -3
  200. package/src/engine-client/actor-http-client.ts +29 -20
  201. package/src/engine-client/actor-websocket-client.ts +6 -3
  202. package/src/engine-client/api-endpoints.ts +72 -14
  203. package/src/engine-client/driver.ts +8 -15
  204. package/src/engine-client/mod.ts +248 -112
  205. package/src/inspector/client.browser.ts +134 -16
  206. package/src/inspector/mod.ts +1 -1
  207. package/src/inspector/workflow.ts +61 -0
  208. package/src/inspector-tab/mod.ts +1 -15
  209. package/src/registry/config/envoy.ts +2 -18
  210. package/src/registry/config/index.ts +54 -61
  211. package/src/registry/index.ts +324 -21
  212. package/src/registry/napi-runtime.ts +583 -39
  213. package/src/registry/native-http.ts +225 -0
  214. package/src/registry/native.ts +1365 -414
  215. package/src/registry/run-handler-coordinator.ts +219 -0
  216. package/src/registry/runtime.ts +372 -7
  217. package/src/registry/wasm-runtime.ts +490 -21
  218. package/src/registry/write-through-proxy.ts +101 -0
  219. package/src/test/mod.ts +1 -19
  220. package/src/test/ready.ts +22 -0
  221. package/src/unstable/migrations.ts +141 -0
  222. package/src/utils/env-vars.ts +15 -6
  223. package/src/utils/node.ts +44 -28
  224. package/src/utils.ts +3 -0
  225. package/src/workflow/context.ts +9 -27
  226. package/src/workflow/driver.ts +253 -116
  227. package/src/workflow/inspector.ts +23 -24
  228. package/src/workflow/mod.ts +50 -72
  229. package/dist/tsup/chunk-2CICCOBL.cjs.map +0 -1
  230. package/dist/tsup/chunk-2WMYYG4R.cjs.map +0 -1
  231. package/dist/tsup/chunk-3MHDOUD7.cjs.map +0 -1
  232. package/dist/tsup/chunk-4FC7TVS6.cjs.map +0 -1
  233. package/dist/tsup/chunk-4PX3ZKJR.js.map +0 -1
  234. package/dist/tsup/chunk-75UE45DE.js.map +0 -1
  235. package/dist/tsup/chunk-7QKCGUTJ.cjs.map +0 -1
  236. package/dist/tsup/chunk-AGDY6TKH.js.map +0 -1
  237. package/dist/tsup/chunk-HXUEHHJF.js.map +0 -1
  238. package/dist/tsup/chunk-JZ7TWV65.js.map +0 -1
  239. package/dist/tsup/chunk-ND567FR5.cjs.map +0 -1
  240. package/dist/tsup/chunk-NHUTYWKU.cjs.map +0 -1
  241. package/dist/tsup/chunk-QAVN4QNO.cjs.map +0 -1
  242. package/dist/tsup/chunk-RIT7N3TX.js.map +0 -1
  243. package/dist/tsup/chunk-RV5MAFDT.cjs +0 -382
  244. package/dist/tsup/chunk-RV5MAFDT.cjs.map +0 -1
  245. package/dist/tsup/chunk-VE2X4KMG.cjs.map +0 -1
  246. package/dist/tsup/chunk-XFYEVYHG.js.map +0 -1
  247. package/dist/tsup/chunk-XHGDS3DZ.js +0 -382
  248. package/dist/tsup/chunk-XHGDS3DZ.js.map +0 -1
  249. package/dist/tsup/chunk-XIX5DOZN.js.map +0 -1
  250. package/dist/tsup/chunk-ZA7FLHKH.cjs.map +0 -1
  251. package/dist/tsup/chunk-ZZ3WBRPD.js.map +0 -1
  252. package/src/actor/driver.ts +0 -141
  253. package/src/actor/instance/mod.ts +0 -2484
  254. package/src/actor/keys.test.ts +0 -275
  255. package/src/actor/utils.test.ts +0 -48
  256. package/src/agent-os/actor/cron.ts +0 -84
  257. package/src/agent-os/actor/db.ts +0 -54
  258. package/src/agent-os/actor/filesystem.ts +0 -131
  259. package/src/agent-os/actor/index.ts +0 -285
  260. package/src/agent-os/actor/network.ts +0 -58
  261. package/src/agent-os/actor/preview.ts +0 -190
  262. package/src/agent-os/actor/process.ts +0 -177
  263. package/src/agent-os/actor/session.ts +0 -518
  264. package/src/agent-os/actor/shell.ts +0 -60
  265. package/src/agent-os/config.ts +0 -79
  266. package/src/agent-os/fs/database-vfs.ts +0 -624
  267. package/src/agent-os/types.ts +0 -148
  268. package/src/client/test.ts +0 -44
  269. package/src/common/database/mod.test.ts +0 -137
  270. package/src/common/database/native-database.test.ts +0 -211
  271. package/src/common/inline-websocket-adapter.test.ts +0 -51
  272. package/src/drivers/engine/actor-driver.ts +0 -2473
  273. package/src/registry/process-metrics.ts +0 -250
  274. package/src/registry/runtime.test.ts +0 -50
  275. package/src/utils/endpoint-parser.test.ts +0 -206
  276. package/src/utils.test.ts +0 -34
  277. /package/dist/tsup/{chunk-IICIOTML.js.map → chunk-OX5MX7TO.js.map} +0 -0
@@ -23,6 +23,7 @@ import {
23
23
  import { assertUnreachable, stringifyError } from "@/common/utils";
24
24
  import type { UniversalWebSocket } from "@/common/websocket-interface";
25
25
  import type { EngineControlClient } from "@/engine-client/driver";
26
+ import type { CurrentActorInvocation } from "@/registry/runtime";
26
27
  import {
27
28
  decodeCborCompat,
28
29
  deserializeWithEncoding,
@@ -50,6 +51,7 @@ import { ACTOR_CONNS_SYMBOL, type ClientRaw } from "./client";
50
51
  import * as errors from "./errors";
51
52
  import { isRetryableLifecycleReconnectSignal } from "./lifecycle-errors";
52
53
  import { logger } from "./log";
54
+ import { outboundTelemetryHeaders } from "./outbound-telemetry";
53
55
  import {
54
56
  createQueueSender,
55
57
  type QueueSendNoWaitOptions,
@@ -57,6 +59,8 @@ import {
57
59
  type QueueSendResult,
58
60
  type QueueSendWaitOptions,
59
61
  } from "./queue";
62
+ import { retryAfterMs } from "./retry-budget";
63
+ import { isInvalidToken } from "./token-provider";
60
64
  import {
61
65
  type WebSocketMessage as ConnMessage,
62
66
  messageLength,
@@ -101,6 +105,21 @@ interface EventSubscriptions<Args extends Array<unknown>> {
101
105
  */
102
106
  export type EventUnsubscribe = () => void;
103
107
 
108
+ /**
109
+ * How long a connection attempt may wait for the actor's Init before it is abandoned and
110
+ * retried. Longer than the engine's own WebSocket open deadline (15 s by default), so an engine
111
+ * that answers with a typed close wins, while a socket that never settles is still retried.
112
+ */
113
+ const CONNECT_TIMEOUT_MS = 20_000;
114
+
115
+ function isConnectionOpenTimeout(error: unknown): boolean {
116
+ return (
117
+ error instanceof errors.ActorError &&
118
+ error.group === "client" &&
119
+ error.code === "connection_open_timeout"
120
+ );
121
+ }
122
+
104
123
  /**
105
124
  * A function that handles connection errors.
106
125
  *
@@ -141,6 +160,8 @@ export class ActorConnRaw {
141
160
  #abortController = new AbortController();
142
161
 
143
162
  #connStatus: ActorConnStatus = "idle";
163
+ /** The opening failure that stopped retries. Later actions must fail instead of queueing. */
164
+ #connectionOpenError?: errors.ActorError;
144
165
 
145
166
  #actorId?: string;
146
167
  #connId?: string;
@@ -179,11 +200,15 @@ export class ActorConnRaw {
179
200
 
180
201
  /** Promise used to indicate the socket has connected successfully. This will be rejected if the connection fails. */
181
202
  #onOpenPromise?: ReturnType<typeof promiseWithResolvers<undefined>>;
203
+ /** Sockets given up on by the connect timeout. Their late events are ignored. */
204
+ #abandonedSockets = new WeakSet<UniversalWebSocket>();
182
205
 
183
206
  #websocket?: UniversalWebSocket;
207
+ #authRetryAvailable = true;
184
208
 
185
209
  #client: ClientRaw;
186
210
  #driver: EngineControlClient;
211
+ #currentActorInvocation?: CurrentActorInvocation;
187
212
  #params: unknown;
188
213
  #getParams?: () => Promise<unknown>;
189
214
  #encoding: Encoding;
@@ -207,8 +232,10 @@ export class ActorConnRaw {
207
232
  encoding: Encoding,
208
233
  actorResolutionState: ActorResolutionState,
209
234
  gatewayOptions: ActorGatewayOptions = {},
235
+ currentActorInvocation?: CurrentActorInvocation,
210
236
  ) {
211
237
  this.#client = client;
238
+ this.#currentActorInvocation = currentActorInvocation;
212
239
  this.#driver = driver;
213
240
  this.#params = params;
214
241
  this.#getParams = getParams;
@@ -229,6 +256,8 @@ export class ActorConnRaw {
229
256
  this.#queueSender = createQueueSender({
230
257
  encoding: this.#encoding,
231
258
  params: this.#params,
259
+ telemetryHeaders: () =>
260
+ outboundTelemetryHeaders(this.#currentActorInvocation?.()),
232
261
  customFetch: async (request: Request) => {
233
262
  return await this.#driver.sendRequest(
234
263
  getGatewayTarget(this.#actorResolutionState),
@@ -238,7 +267,7 @@ export class ActorConnRaw {
238
267
  },
239
268
  });
240
269
 
241
- this.#keepNodeAliveInterval = setInterval(() => 60_000);
270
+ this.#keepNodeAliveInterval = setInterval(() => {}, 60_000);
242
271
  }
243
272
 
244
273
  #clearResolvedActorIdentity() {
@@ -329,6 +358,8 @@ export class ActorConnRaw {
329
358
  `Invalid action call: expected an options object { name, args }, got ${typeof opts}. Use conn.actionName(...args) for the shorthand API.`,
330
359
  );
331
360
  }
361
+ if (this.#disposed) throw new errors.ActorConnDisposed();
362
+ if (this.#connectionOpenError) throw this.#connectionOpenError;
332
363
  logger().debug({ msg: "action", name: opts.name, args: opts.args });
333
364
 
334
365
  // If we have an active connection, use the websockactionId
@@ -439,6 +470,7 @@ export class ActorConnRaw {
439
470
  }
440
471
 
441
472
  #connectWithRetry() {
473
+ this.#connectionOpenError = undefined;
442
474
  this.#setConnStatus("connecting");
443
475
 
444
476
  // Attempt to reconnect indefinitely
@@ -448,12 +480,17 @@ export class ActorConnRaw {
448
480
  minTimeout: 250,
449
481
  maxTimeout: 30_000,
450
482
 
451
- onFailedAttempt: (error) => {
483
+ onFailedAttempt: async (error) => {
452
484
  logger().warn({
453
485
  msg: "failed to reconnect",
454
486
  attempt: error.attemptNumber,
455
487
  error: stringifyError(error),
456
488
  });
489
+ // A close that carried the service's retry hint waits at least that long.
490
+ const hintMs = retryAfterMs(error);
491
+ if (hintMs !== undefined) {
492
+ await new Promise((resolve) => setTimeout(resolve, hintMs));
493
+ }
457
494
  },
458
495
 
459
496
  // Cancel retry if aborted
@@ -489,8 +526,13 @@ export class ActorConnRaw {
489
526
  await this.#connectWebSocket();
490
527
 
491
528
  // Wait for result
492
- await this.#onOpenPromise.promise;
529
+ await this.#waitForOpen(this.#onOpenPromise.promise);
493
530
  } catch (error) {
531
+ this.#abandonCurrentSocket(
532
+ isConnectionOpenTimeout(error)
533
+ ? "Connect timeout"
534
+ : "Connection opening failed",
535
+ );
494
536
  if (this.#shouldRetryConnectionOpenError(error)) {
495
537
  throw error;
496
538
  }
@@ -505,7 +547,7 @@ export class ActorConnRaw {
505
547
  { error: stringifyError(error) },
506
548
  );
507
549
 
508
- this.#clearQueuedMessages();
550
+ this.#connectionOpenError = actorError;
509
551
  this.#rejectPendingPromises(actorError, false);
510
552
  this.#dispatchActorError(actorError);
511
553
  this.#setConnStatus("idle");
@@ -520,10 +562,76 @@ export class ActorConnRaw {
520
562
  }
521
563
  }
522
564
 
565
+ /**
566
+ * Waits for the actor's Init on the current socket, up to {@link CONNECT_TIMEOUT_MS}. A socket
567
+ * can stay connecting with no open, error or close event, as when the engine holds the
568
+ * upgrade or a refused connect reports only an error. On timeout the socket is abandoned
569
+ * and closed, and the retryable `client.connection_open_timeout` starts the next attempt.
570
+ */
571
+ async #waitForOpen(open: Promise<undefined>) {
572
+ let timer: ReturnType<typeof setTimeout> | undefined;
573
+ const timedOut = new Promise<never>((_, reject) => {
574
+ timer = setTimeout(() => {
575
+ reject(
576
+ new errors.ActorError(
577
+ "client",
578
+ "connection_open_timeout",
579
+ `Connection did not open within ${CONNECT_TIMEOUT_MS} ms`,
580
+ ),
581
+ );
582
+ }, CONNECT_TIMEOUT_MS);
583
+ });
584
+ try {
585
+ await Promise.race([open, timedOut]);
586
+ } finally {
587
+ clearTimeout(timer);
588
+ }
589
+ }
590
+
591
+ #abandonCurrentSocket(reason: string) {
592
+ const ws = this.#websocket;
593
+ if (!ws) return;
594
+ this.#websocket = undefined;
595
+ this.#abandonedSockets.add(ws);
596
+ logger().debug({
597
+ msg: "closing failed connection attempt",
598
+ connId: this.#connId,
599
+ readyState: ws.readyState,
600
+ reason,
601
+ });
602
+ try {
603
+ ws.close(1000, reason);
604
+ } catch (err) {
605
+ logger().debug({
606
+ msg: "failed to close abandoned websocket",
607
+ error: stringifyError(err),
608
+ });
609
+ }
610
+ }
611
+
523
612
  #shouldRetryConnectionOpenError(error: unknown): boolean {
524
613
  if (error instanceof errors.ActorConnDisposed) {
525
614
  return false;
526
615
  }
616
+ if (isConnectionOpenTimeout(error)) {
617
+ return true;
618
+ }
619
+ if (
620
+ error instanceof errors.ActorError &&
621
+ error.group === "client" &&
622
+ error.code === "connection_open_interrupted"
623
+ ) {
624
+ return true;
625
+ }
626
+ if (
627
+ error instanceof errors.ActorError &&
628
+ isInvalidToken(error.group, error.code) &&
629
+ this.#driver.refreshAuthToken &&
630
+ this.#authRetryAvailable
631
+ ) {
632
+ this.#authRetryAvailable = false;
633
+ return true;
634
+ }
527
635
 
528
636
  if (
529
637
  error instanceof errors.ActorError &&
@@ -596,14 +704,18 @@ export class ActorConnRaw {
596
704
  });
597
705
  this.#websocket = ws;
598
706
  ws.addEventListener("open", () => {
707
+ if (this.#websocket !== ws || this.#abandonedSockets.has(ws))
708
+ return;
599
709
  logger().debug({
600
710
  msg: "client websocket open",
601
711
  connId: this.#connId,
602
712
  });
603
713
  });
604
714
  ws.addEventListener("message", async (ev: { data: ConnMessage }) => {
715
+ if (this.#websocket !== ws || this.#abandonedSockets.has(ws))
716
+ return;
605
717
  try {
606
- await this.#handleOnMessage(ev.data);
718
+ await this.#handleOnMessage(ev.data, ws);
607
719
  } catch (err) {
608
720
  logger().error({
609
721
  msg: "error in websocket message handler",
@@ -612,6 +724,8 @@ export class ActorConnRaw {
612
724
  }
613
725
  });
614
726
  ws.addEventListener("close", async (ev: Event | CloseEventLike) => {
727
+ if (this.#websocket !== ws || this.#abandonedSockets.has(ws))
728
+ return;
615
729
  try {
616
730
  await this.#handleOnClose(ev);
617
731
  } catch (err) {
@@ -622,6 +736,8 @@ export class ActorConnRaw {
622
736
  }
623
737
  });
624
738
  ws.addEventListener("error", () => {
739
+ if (this.#websocket !== ws || this.#abandonedSockets.has(ws))
740
+ return;
625
741
  try {
626
742
  this.#handleOnError();
627
743
  } catch (err) {
@@ -672,6 +788,7 @@ export class ActorConnRaw {
672
788
  });
673
789
 
674
790
  // Update connection state (this also notifies handlers)
791
+ this.#authRetryAvailable = true;
675
792
  this.#setConnStatus("connected");
676
793
 
677
794
  // Resolve open promise
@@ -702,7 +819,7 @@ export class ActorConnRaw {
702
819
  }
703
820
 
704
821
  /** Called by the onmessage event from drivers. */
705
- async #handleOnMessage(data: any) {
822
+ async #handleOnMessage(data: any, socket: UniversalWebSocket) {
706
823
  logger().trace({
707
824
  msg: "received message",
708
825
  dataType: typeof data,
@@ -711,6 +828,10 @@ export class ActorConnRaw {
711
828
  });
712
829
 
713
830
  const response = await this.#parseMessage(data as ConnMessage);
831
+ // Decoding can yield while this socket closes and another connects. Apply
832
+ // the message only while its socket still owns the connection.
833
+ if (this.#websocket !== socket || this.#abandonedSockets.has(socket))
834
+ return;
714
835
  logger().trace(
715
836
  getLogMessage()
716
837
  ? {
@@ -796,23 +917,19 @@ export class ActorConnRaw {
796
917
  this.#actorResolutionState,
797
918
  this.#driver,
798
919
  );
920
+ if (
921
+ this.#websocket !== socket ||
922
+ this.#abandonedSockets.has(socket)
923
+ ) {
924
+ return;
925
+ }
799
926
  if (schedulingError) {
800
927
  errorToThrow = schedulingError;
801
928
  }
802
929
  }
803
930
 
804
- // If we have an onOpenPromise, reject it with the error
805
- if (this.#onOpenPromise) {
806
- this.#onOpenPromise.reject(errorToThrow);
807
- }
808
-
809
931
  this.#invalidateActorIfStale(group, code);
810
-
811
- // Reject any in-flight requests
812
- for (const [id, inFlight] of this.#actionsInFlight.entries()) {
813
- inFlight.reject(errorToThrow);
814
- this.#actionsInFlight.delete(id);
815
- }
932
+ this.#rejectPendingPromises(errorToThrow, false);
816
933
 
817
934
  this.#dispatchActorError(errorToThrow);
818
935
  }
@@ -846,6 +963,7 @@ export class ActorConnRaw {
846
963
 
847
964
  /** Called by the onclose event from drivers. */
848
965
  async #handleOnClose(event: Event | CloseEventLike) {
966
+ const failedSocket = this.#websocket;
849
967
  // We can't use `event instanceof CloseEvent` because it's not defined in NodeJS
850
968
  const closeEvent = event as CloseEventLike;
851
969
  const wasClean = closeEvent.wasClean;
@@ -874,7 +992,31 @@ export class ActorConnRaw {
874
992
  const parsed = parseWebSocketCloseReason(reason);
875
993
 
876
994
  if (parsed) {
877
- const { group, code } = parsed;
995
+ const { group, code, rayId, retryAfterMs } = parsed;
996
+ const closeOptions =
997
+ retryAfterMs === undefined
998
+ ? { rayId }
999
+ : { rayId, metadata: { retry_after_ms: retryAfterMs } };
1000
+ if (
1001
+ isInvalidToken(group, code) &&
1002
+ closeEvent.code === 1008 &&
1003
+ failedSocket
1004
+ ) {
1005
+ try {
1006
+ if (
1007
+ !(await this.#driver.refreshAuthToken?.(
1008
+ failedSocket,
1009
+ ))
1010
+ ) {
1011
+ this.#authRetryAvailable = false;
1012
+ }
1013
+ } catch {
1014
+ logger().warn("token renewal failed");
1015
+ this.#authRetryAvailable = false;
1016
+ }
1017
+ } else if (isInvalidToken(group, code)) {
1018
+ this.#authRetryAvailable = false;
1019
+ }
878
1020
 
879
1021
  if (this.#shouldReconnectForStaleActor(group, code)) {
880
1022
  this.#clearResolvedActorIdentity();
@@ -883,7 +1025,7 @@ export class ActorConnRaw {
883
1025
  group,
884
1026
  code,
885
1027
  `Connection closed: ${reason}`,
886
- undefined,
1028
+ closeOptions,
887
1029
  ),
888
1030
  );
889
1031
  return;
@@ -897,6 +1039,7 @@ export class ActorConnRaw {
897
1039
  this.#actorId,
898
1040
  this.#actorResolutionState,
899
1041
  this.#driver,
1042
+ rayId,
900
1043
  );
901
1044
  if (schedulingError) {
902
1045
  error = schedulingError;
@@ -905,7 +1048,7 @@ export class ActorConnRaw {
905
1048
  group,
906
1049
  code,
907
1050
  `Connection closed: ${reason}`,
908
- undefined,
1051
+ closeOptions,
909
1052
  );
910
1053
  }
911
1054
  } else {
@@ -913,16 +1056,24 @@ export class ActorConnRaw {
913
1056
  group,
914
1057
  code,
915
1058
  `Connection closed: ${reason}`,
916
- undefined,
1059
+ closeOptions,
917
1060
  );
918
1061
  }
919
1062
 
920
1063
  this.#invalidateActorIfStale(group, code);
921
1064
  } else {
922
1065
  // Default error for non-structured close reasons
923
- error = new Error(
924
- `${wasClean ? "Connection closed" : "Connection lost"} (code: ${closeEvent.code}, reason: ${reason})`,
925
- );
1066
+ const message = `${wasClean ? "Connection closed" : "Connection lost"} (code: ${closeEvent.code}, reason: ${reason})`;
1067
+ // A transport loss during the handshake can outlast an Engine
1068
+ // restart. Retry opening without replaying actions with unknown outcomes.
1069
+ error =
1070
+ !wasConnected && closeEvent.code === 1006
1071
+ ? new errors.ActorError(
1072
+ "client",
1073
+ "connection_open_interrupted",
1074
+ message,
1075
+ )
1076
+ : new Error(message);
926
1077
  }
927
1078
 
928
1079
  this.#rejectPendingPromises(error, false);
@@ -933,7 +1084,7 @@ export class ActorConnRaw {
933
1084
  }
934
1085
 
935
1086
  // Automatically reconnect if we were connected
936
- if (wasConnected) {
1087
+ if (wasConnected && this.#authRetryAvailable) {
937
1088
  logger().debug({
938
1089
  msg: "triggering reconnect",
939
1090
  connId: this.#connId,
@@ -944,6 +1095,8 @@ export class ActorConnRaw {
944
1095
  }
945
1096
 
946
1097
  #rejectPendingPromises(error: Error, suppressUnhandled: boolean) {
1098
+ // Rejected actions must not survive in the queue and execute after a reconnect.
1099
+ this.#clearQueuedMessages();
947
1100
  if (this.#onOpenPromise) {
948
1101
  if (suppressUnhandled) {
949
1102
  this.#onOpenPromise.promise.catch(() => {});