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
@@ -0,0 +1,78 @@
1
+ import { ActorError } from "./errors";
2
+
3
+ /** How many retries a client may spend, as a share of the requests it sends. */
4
+ export interface RetryBudgetOptions {
5
+ /** Retries allowed per 100 requests (default 10). */
6
+ percent?: number;
7
+ /** Retries the client can bank for a burst of refusals; the budget starts full (default 100). */
8
+ maxTokens?: number;
9
+ }
10
+
11
+ const DEFAULT_PERCENT = 10;
12
+ const DEFAULT_MAX_TOKENS = 100;
13
+
14
+ /**
15
+ * Bounds the client's retries of refused requests. Each request adds a fraction of a token and
16
+ * each retry spends one, so retries stay a fixed share of traffic while the service sheds load
17
+ * instead of multiplying it.
18
+ */
19
+ export class RetryBudget {
20
+ #tokens: number;
21
+ readonly #maxTokens: number;
22
+ readonly #deposit: number;
23
+
24
+ constructor(options: RetryBudgetOptions = {}) {
25
+ const percent = options.percent ?? DEFAULT_PERCENT;
26
+ const maxTokens = options.maxTokens ?? DEFAULT_MAX_TOKENS;
27
+ if (!(percent >= 0 && percent <= 100)) {
28
+ throw new Error("retryBudget.percent must be in 0..=100");
29
+ }
30
+ if (!(maxTokens >= 0)) {
31
+ throw new Error("retryBudget.maxTokens must be at least 0");
32
+ }
33
+ this.#maxTokens = maxTokens;
34
+ this.#tokens = maxTokens;
35
+ this.#deposit = percent / 100;
36
+ }
37
+
38
+ /** Credits one request sent. */
39
+ deposit(): void {
40
+ this.#tokens = Math.min(this.#maxTokens, this.#tokens + this.#deposit);
41
+ }
42
+
43
+ /** Spends one token. Returns `false` when the budget is spent. */
44
+ tryWithdraw(): boolean {
45
+ // Rounding guards against float drift: ten 0.1 deposits must make one token.
46
+ if (this.#tokens + 1e-9 < 1) {
47
+ return false;
48
+ }
49
+ this.#tokens = Math.max(0, this.#tokens - 1);
50
+ return true;
51
+ }
52
+ }
53
+
54
+ /** The service's retry hint for a refused request, in milliseconds, when it sent one. */
55
+ export function retryAfterMs(error: unknown): number | undefined {
56
+ if (!(error instanceof ActorError)) {
57
+ return undefined;
58
+ }
59
+ const metadata = error.metadata;
60
+ if (metadata === null || typeof metadata !== "object") {
61
+ return undefined;
62
+ }
63
+ const hint = (metadata as { retry_after_ms?: unknown }).retry_after_ms;
64
+ return typeof hint === "number" && hint >= 0 ? hint : undefined;
65
+ }
66
+
67
+ /**
68
+ * The wait before retry `attempt` (0-based): jittered exponential backoff over
69
+ * `[initialDelayMs, maxDelayMs]`, never shorter than the service's hint.
70
+ */
71
+ export function retryDelayMs(
72
+ attempt: number,
73
+ opts: { initialDelayMs: number; maxDelayMs: number; hintMs?: number },
74
+ ): number {
75
+ const backoff = Math.min(opts.initialDelayMs * 2 ** attempt, opts.maxDelayMs);
76
+ const jittered = backoff / 2 + Math.random() * (backoff / 2);
77
+ return Math.max(jittered, opts.hintMs ?? 0);
78
+ }
@@ -0,0 +1,93 @@
1
+ /** Called when RivetKit needs a credential. Keep issuer secrets on your backend. */
2
+ export type GetToken = (options: { forceRefresh: boolean }) => Promise<string>;
3
+
4
+ /** Only a confirmed invalid/expired credential can trigger an automatic renewal. */
5
+ export function isInvalidToken(group: string, code: string): boolean {
6
+ return (
7
+ group === "auth" &&
8
+ (code === "invalid_token" || code === "token_expired")
9
+ );
10
+ }
11
+
12
+ export function isInvalidTokenResponse(response: Response): boolean {
13
+ if (response.status !== 401) return false;
14
+ const error = response.headers.get("x-rivet-error");
15
+ if (!error) return false;
16
+ const separator = error.indexOf(".");
17
+ return (
18
+ separator > 0 &&
19
+ isInvalidToken(error.slice(0, separator), error.slice(separator + 1))
20
+ );
21
+ }
22
+
23
+ /** One token cache per client; concurrent requests and refreshes share the same issuance call. */
24
+ export class TokenProvider {
25
+ #cached?: { token: string; expiresAt?: number };
26
+ #pending?: Promise<string>;
27
+ #pendingForce = false;
28
+
29
+ constructor(private readonly getToken: GetToken) {}
30
+
31
+ async current(): Promise<string> {
32
+ if (
33
+ this.#cached &&
34
+ (this.#cached.expiresAt === undefined ||
35
+ Date.now() < this.#cached.expiresAt - 5_000)
36
+ ) {
37
+ return this.#cached.token;
38
+ }
39
+ return this.#load(false);
40
+ }
41
+
42
+ /** A late rejection for an older token must never erase a newer credential. */
43
+ async refreshIfCurrent(failedToken: string): Promise<string> {
44
+ if (this.#pending !== undefined) {
45
+ const wasForced = this.#pendingForce;
46
+ const refreshed = await this.#pending;
47
+ if (wasForced || refreshed !== failedToken) return refreshed;
48
+ }
49
+ if (this.#cached?.token !== failedToken) {
50
+ return this.current();
51
+ }
52
+ return this.#load(true);
53
+ }
54
+
55
+ #load(forceRefresh: boolean): Promise<string> {
56
+ if (this.#pending !== undefined) return this.#pending;
57
+
58
+ const pending = Promise.resolve()
59
+ .then(() => this.getToken({ forceRefresh }))
60
+ .then((token) => {
61
+ if (typeof token !== "string" || token.length === 0) {
62
+ throw new Error(
63
+ "getToken must resolve to a nonempty string",
64
+ );
65
+ }
66
+ this.#cached = { token, expiresAt: jwtExpiration(token) };
67
+ return token;
68
+ })
69
+ .finally(() => {
70
+ if (this.#pending === pending) this.#pending = undefined;
71
+ });
72
+ this.#pending = pending;
73
+ this.#pendingForce = forceRefresh;
74
+ return pending;
75
+ }
76
+ }
77
+
78
+ // This is only a cache hint, never a signature or authorization check. The Engine verifies JWTs.
79
+ function jwtExpiration(token: string): number | undefined {
80
+ try {
81
+ const payload = token.split(".")[1];
82
+ if (!payload) return undefined;
83
+ const claims = JSON.parse(
84
+ atob(payload.replace(/-/g, "+").replace(/_/g, "/")),
85
+ ) as { exp?: unknown };
86
+ return typeof claims.exp === "number" &&
87
+ Number.isSafeInteger(claims.exp)
88
+ ? claims.exp * 1_000
89
+ : undefined;
90
+ } catch {
91
+ return undefined;
92
+ }
93
+ }
@@ -19,21 +19,27 @@ export interface ParsedCloseReason {
19
19
  group: string;
20
20
  code: string;
21
21
  rayId?: string;
22
+ /** The service's retry hint, from a `#retry_after_ms=<ms>` suffix. */
23
+ retryAfterMs?: number;
22
24
  }
23
25
 
26
+ const RETRY_AFTER_SUFFIX = /^retry_after_ms=(\d+)$/;
27
+
24
28
  /**
25
29
  * Parses WebSocket close reason string into structured data.
26
30
  *
27
31
  * Expected format examples:
28
32
  * - "guard.actor_runner_failed#t1s80so6h3irenp8ymzltfoittcl00"
29
33
  * - "ws.client_closed"
34
+ * - "guard.service_unavailable#retry_after_ms=1000", a refusal with its retry hint. The hint
35
+ * rides after `#` so older clients still read the retryable `<group>.<code>`.
30
36
  *
31
37
  * Returns undefined if the format is invalid
32
38
  */
33
39
  export function parseWebSocketCloseReason(
34
40
  reason: string,
35
41
  ): ParsedCloseReason | undefined {
36
- const [mainPart, rayId] = reason.split("#");
42
+ const [mainPart, suffix] = reason.split("#");
37
43
  const [group, code] = mainPart.split(".");
38
44
 
39
45
  if (!group || !code) {
@@ -41,10 +47,15 @@ export function parseWebSocketCloseReason(
41
47
  return undefined;
42
48
  }
43
49
 
50
+ const retryAfter = suffix?.match(RETRY_AFTER_SUFFIX);
51
+ if (retryAfter) {
52
+ return { group, code, retryAfterMs: Number(retryAfter[1]) };
53
+ }
54
+
44
55
  return {
45
56
  group,
46
57
  code,
47
- rayId,
58
+ rayId: suffix,
48
59
  };
49
60
  }
50
61
 
@@ -215,6 +226,7 @@ export async function sendHttpRequest<
215
226
  code: responseData.code,
216
227
  message: responseData.message,
217
228
  metadata: responseData.metadata,
229
+ rayId,
218
230
  actorId: responseData.actor?.actorId,
219
231
  generation: responseData.actor?.generation,
220
232
  actorKey: responseData.actor?.key,
@@ -226,7 +238,9 @@ export async function sendHttpRequest<
226
238
  responseData.message,
227
239
  {
228
240
  metadata: responseData.metadata,
241
+ rayId: rayId ?? undefined,
229
242
  actor: responseData.actor,
243
+ statusCode: response.status,
230
244
  },
231
245
  );
232
246
  } catch (error) {
@@ -12,12 +12,17 @@ export const HEADER_ENCODING = "x-rivet-encoding";
12
12
 
13
13
  // IMPORTANT: Params must be in headers or in an E2EE part of the request (i.e. NOT the URL or query string) in order to ensure that tokens can be securely passed in params.
14
14
  export const HEADER_CONN_PARAMS = "x-rivet-conn-params";
15
+ export const HEADER_ORIGINAL_REQUEST_URL =
16
+ "x-rivet-internal-original-request-url";
15
17
 
16
18
  export const HEADER_ACTOR_ID = "x-rivet-actor";
17
19
  export const HEADER_ACTOR_GENERATION = "x-rivet-actor-generation";
18
20
  export const HEADER_ACTOR_KEY = "x-rivet-actor-key";
19
21
 
20
22
  export const HEADER_RIVET_TOKEN = "x-rivet-token";
23
+ export const HEADER_RIVET_RAY_ID = "x-rivet-ray-id";
24
+ export const HEADER_TRACEPARENT = "traceparent";
25
+ export const HEADER_TRACESTATE = "tracestate";
21
26
 
22
27
  // MARK: Manager Gateway Headers
23
28
  export const HEADER_RIVET_TARGET = "x-rivet-target";
@@ -0,0 +1,19 @@
1
+ /** Correlation owned by the currently executing Core actor invocation. */
2
+ export interface ActorInvocationTraceContext {
3
+ /** Ray ID the invocation received, absent when nothing upstream supplied one. */
4
+ readonly rayId?: string;
5
+ /** Core-owned invocation span context, absent when tracing is disabled. */
6
+ readonly span?: ActorInvocationSpanContext;
7
+ }
8
+
9
+ /** W3C span context of the Core invocation span. */
10
+ export interface ActorInvocationSpanContext {
11
+ /** W3C trace identifier. */
12
+ readonly traceId: string;
13
+ /** W3C span identifier for the Core invocation. */
14
+ readonly spanId: string;
15
+ /** OpenTelemetry trace flags encoded as an integer. */
16
+ readonly traceFlags: number;
17
+ /** Optional vendor trace state inherited by the Core invocation. */
18
+ readonly tracestate?: string;
19
+ }