@devries-cameron20 / t3code-1 / commits / 9ebca5f71c
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(relay): tunnel limit counts abandoned reprovisions and frees failed revokes (#26320)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 28, 2026 at 00:13 UTC 9ebca5f71cbcf050e94732f0a1da041073223c84
10 files changed +1931 -67
infra/relay/migrations/postgres/20261028062605_backfill_withdrawn_tunnel_allocations/migration.sql new
+12
@@ -0,0 +1,12 @@
1 +-- Teardown claims used to bump updated_at past the link's revoke or publish-only write, so a
2 +-- failed deprovision now reads as an unfinished relink. Give those rows the updated_at the
3 +-- current write rule would have left. Touches only withdrawn links' live tunnels; idempotent.
4 +UPDATE "relay_managed_endpoint_allocations" AS a
5 +SET "updated_at" = l."updated_at"
6 +FROM "relay_environment_links" AS l
7 +WHERE l."user_id" = a."user_id"
8 + AND l."environment_id" = a."environment_id"
9 + AND a."tunnel_id" IS NOT NULL
10 + AND a."tunnel_released_at" IS NULL
11 + AND (l."revoked_at" IS NOT NULL OR l."managed_tunnels_enabled" = false)
12 + AND l."updated_at" < a."updated_at";
infra/relay/migrations/postgres/20261028062605_backfill_withdrawn_tunnel_allocations/snapshot.json new
+1571
@@ -0,0 +1,1571 @@
1 +{
2 + "id": "4413755a-63f1-4f93-bdc9-d1357283c23e",
3 + "prevIds": ["7d8c1702-6f47-4453-a8ee-66ce5e3bfb41"],
4 + "version": "8",
5 + "dialect": "postgres",
6 + "ddl": [
7 + {
8 + "isRlsEnabled": false,
9 + "name": "relay_agent_activity_rows",
10 + "entityType": "tables",
11 + "schema": "public"
12 + },
13 + {
14 + "isRlsEnabled": false,
15 + "name": "relay_delivery_attempts",
16 + "entityType": "tables",
17 + "schema": "public"
18 + },
19 + {
20 + "isRlsEnabled": false,
21 + "name": "relay_dpop_proofs",
22 + "entityType": "tables",
23 + "schema": "public"
24 + },
25 + {
26 + "isRlsEnabled": false,
27 + "name": "relay_environment_credentials",
28 + "entityType": "tables",
29 + "schema": "public"
30 + },
31 + {
32 + "isRlsEnabled": false,
33 + "name": "relay_environment_links",
34 + "entityType": "tables",
35 + "schema": "public"
36 + },
37 + {
38 + "isRlsEnabled": false,
39 + "name": "relay_live_activities",
40 + "entityType": "tables",
41 + "schema": "public"
42 + },
43 + {
44 + "isRlsEnabled": false,
45 + "name": "relay_managed_endpoint_allocations",
46 + "entityType": "tables",
47 + "schema": "public"
48 + },
49 + {
50 + "isRlsEnabled": false,
51 + "name": "relay_managed_tunnel_limits",
52 + "entityType": "tables",
53 + "schema": "public"
54 + },
55 + {
56 + "isRlsEnabled": false,
57 + "name": "relay_mobile_devices",
58 + "entityType": "tables",
59 + "schema": "public"
60 + },
61 + {
62 + "isRlsEnabled": false,
63 + "name": "relay_upstream_api_pauses",
64 + "entityType": "tables",
65 + "schema": "public"
66 + },
67 + {
68 + "type": "varchar(191)",
69 + "typeSchema": null,
70 + "notNull": true,
71 + "dimensions": 0,
72 + "default": null,
73 + "generated": null,
74 + "identity": null,
75 + "name": "environment_id",
76 + "entityType": "columns",
77 + "schema": "public",
78 + "table": "relay_agent_activity_rows"
79 + },
80 + {
81 + "type": "text",
82 + "typeSchema": null,
83 + "notNull": true,
84 + "dimensions": 0,
85 + "default": null,
86 + "generated": null,
87 + "identity": null,
88 + "name": "environment_public_key",
89 + "entityType": "columns",
90 + "schema": "public",
91 + "table": "relay_agent_activity_rows"
92 + },
93 + {
94 + "type": "varchar(512)",
95 + "typeSchema": null,
96 + "notNull": true,
97 + "dimensions": 0,
98 + "default": null,
99 + "generated": null,
100 + "identity": null,
101 + "name": "thread_id",
102 + "entityType": "columns",
103 + "schema": "public",
104 + "table": "relay_agent_activity_rows"
105 + },
106 + {
107 + "type": "jsonb",
108 + "typeSchema": null,
109 + "notNull": true,
110 + "dimensions": 0,
111 + "default": null,
112 + "generated": null,
113 + "identity": null,
114 + "name": "state_json",
115 + "entityType": "columns",
116 + "schema": "public",
117 + "table": "relay_agent_activity_rows"
118 + },
119 + {
120 + "type": "varchar(64)",
121 + "typeSchema": null,
122 + "notNull": true,
123 + "dimensions": 0,
124 + "default": null,
125 + "generated": null,
126 + "identity": null,
127 + "name": "updated_at",
128 + "entityType": "columns",
129 + "schema": "public",
130 + "table": "relay_agent_activity_rows"
131 + },
132 + {
133 + "type": "varchar(64)",
134 + "typeSchema": null,
135 + "notNull": true,
136 + "dimensions": 0,
137 + "default": null,
138 + "generated": null,
139 + "identity": null,
140 + "name": "created_at",
141 + "entityType": "columns",
142 + "schema": "public",
143 + "table": "relay_agent_activity_rows"
144 + },
145 + {
146 + "type": "varchar(36)",
147 + "typeSchema": null,
148 + "notNull": true,
149 + "dimensions": 0,
150 + "default": null,
151 + "generated": null,
152 + "identity": null,
153 + "name": "id",
154 + "entityType": "columns",
155 + "schema": "public",
156 + "table": "relay_delivery_attempts"
157 + },
158 + {
159 + "type": "varchar(64)",
160 + "typeSchema": null,
161 + "notNull": true,
162 + "dimensions": 0,
163 + "default": null,
164 + "generated": null,
165 + "identity": null,
166 + "name": "created_at",
167 + "entityType": "columns",
168 + "schema": "public",
169 + "table": "relay_delivery_attempts"
170 + },
171 + {
172 + "type": "varchar(255)",
173 + "typeSchema": null,
174 + "notNull": false,
175 + "dimensions": 0,
176 + "default": null,
177 + "generated": null,
178 + "identity": null,
179 + "name": "user_id",
180 + "entityType": "columns",
181 + "schema": "public",
182 + "table": "relay_delivery_attempts"
183 + },
184 + {
185 + "type": "varchar(191)",
186 + "typeSchema": null,
187 + "notNull": false,
188 + "dimensions": 0,
189 + "default": null,
190 + "generated": null,
191 + "identity": null,
192 + "name": "environment_id",
193 + "entityType": "columns",
194 + "schema": "public",
195 + "table": "relay_delivery_attempts"
196 + },
197 + {
198 + "type": "varchar(512)",
199 + "typeSchema": null,
200 + "notNull": false,
201 + "dimensions": 0,
202 + "default": null,
203 + "generated": null,
204 + "identity": null,
205 + "name": "thread_id",
206 + "entityType": "columns",
207 + "schema": "public",
208 + "table": "relay_delivery_attempts"
209 + },
210 + {
211 + "type": "varchar(255)",
212 + "typeSchema": null,
213 + "notNull": false,
214 + "dimensions": 0,
215 + "default": null,
216 + "generated": null,
217 + "identity": null,
218 + "name": "device_id",
219 + "entityType": "columns",
220 + "schema": "public",
221 + "table": "relay_delivery_attempts"
222 + },
223 + {
224 + "type": "varchar(64)",
225 + "typeSchema": null,
226 + "notNull": true,
227 + "dimensions": 0,
228 + "default": null,
229 + "generated": null,
230 + "identity": null,
231 + "name": "kind",
232 + "entityType": "columns",
233 + "schema": "public",
234 + "table": "relay_delivery_attempts"
235 + },
236 + {
237 + "type": "varchar(64)",
238 + "typeSchema": null,
239 + "notNull": false,
240 + "dimensions": 0,
241 + "default": null,
242 + "generated": null,
243 + "identity": null,
244 + "name": "source_job_id",
245 + "entityType": "columns",
246 + "schema": "public",
247 + "table": "relay_delivery_attempts"
248 + },
249 + {
250 + "type": "varchar(16)",
251 + "typeSchema": null,
252 + "notNull": false,
253 + "dimensions": 0,
254 + "default": null,
255 + "generated": null,
256 + "identity": null,
257 + "name": "token_suffix",
258 + "entityType": "columns",
259 + "schema": "public",
260 + "table": "relay_delivery_attempts"
261 + },
262 + {
263 + "type": "integer",
264 + "typeSchema": null,
265 + "notNull": false,
266 + "dimensions": 0,
267 + "default": null,
268 + "generated": null,
269 + "identity": null,
270 + "name": "apns_status",
271 + "entityType": "columns",
272 + "schema": "public",
273 + "table": "relay_delivery_attempts"
274 + },
275 + {
276 + "type": "text",
277 + "typeSchema": null,
278 + "notNull": false,
279 + "dimensions": 0,
280 + "default": null,
281 + "generated": null,
282 + "identity": null,
283 + "name": "apns_reason",
284 + "entityType": "columns",
285 + "schema": "public",
286 + "table": "relay_delivery_attempts"
287 + },
288 + {
289 + "type": "varchar(128)",
290 + "typeSchema": null,
291 + "notNull": false,
292 + "dimensions": 0,
293 + "default": null,
294 + "generated": null,
295 + "identity": null,
296 + "name": "apns_id",
297 + "entityType": "columns",
298 + "schema": "public",
299 + "table": "relay_delivery_attempts"
300 + },
301 + {
302 + "type": "text",
303 + "typeSchema": null,
304 + "notNull": false,
305 + "dimensions": 0,
306 + "default": null,
307 + "generated": null,
308 + "identity": null,
309 + "name": "transport_error",
310 + "entityType": "columns",
311 + "schema": "public",
312 + "table": "relay_delivery_attempts"
313 + },
314 + {
315 + "type": "varchar(128)",
316 + "typeSchema": null,
317 + "notNull": true,
318 + "dimensions": 0,
319 + "default": null,
320 + "generated": null,
321 + "identity": null,
322 + "name": "thumbprint",
323 + "entityType": "columns",
324 + "schema": "public",
325 + "table": "relay_dpop_proofs"
326 + },
327 + {
328 + "type": "varchar(255)",
329 + "typeSchema": null,
330 + "notNull": true,
331 + "dimensions": 0,
332 + "default": null,
333 + "generated": null,
334 + "identity": null,
335 + "name": "jti",
336 + "entityType": "columns",
337 + "schema": "public",
338 + "table": "relay_dpop_proofs"
339 + },
340 + {
341 + "type": "integer",
342 + "typeSchema": null,
343 + "notNull": true,
344 + "dimensions": 0,
345 + "default": null,
346 + "generated": null,
347 + "identity": null,
348 + "name": "iat",
349 + "entityType": "columns",
350 + "schema": "public",
351 + "table": "relay_dpop_proofs"
352 + },
353 + {
354 + "type": "varchar(64)",
355 + "typeSchema": null,
356 + "notNull": true,
357 + "dimensions": 0,
358 + "default": null,
359 + "generated": null,
360 + "identity": null,
361 + "name": "expires_at",
362 + "entityType": "columns",
363 + "schema": "public",
364 + "table": "relay_dpop_proofs"
365 + },
366 + {
367 + "type": "varchar(64)",
368 + "typeSchema": null,
369 + "notNull": true,
370 + "dimensions": 0,
371 + "default": null,
372 + "generated": null,
373 + "identity": null,
374 + "name": "created_at",
375 + "entityType": "columns",
376 + "schema": "public",
377 + "table": "relay_dpop_proofs"
378 + },
379 + {
380 + "type": "varchar(64)",
381 + "typeSchema": null,
382 + "notNull": true,
383 + "dimensions": 0,
384 + "default": null,
385 + "generated": null,
386 + "identity": null,
387 + "name": "credential_id",
388 + "entityType": "columns",
389 + "schema": "public",
390 + "table": "relay_environment_credentials"
391 + },
392 + {
393 + "type": "varchar(191)",
394 + "typeSchema": null,
395 + "notNull": true,
396 + "dimensions": 0,
397 + "default": null,
398 + "generated": null,
399 + "identity": null,
400 + "name": "environment_id",
401 + "entityType": "columns",
402 + "schema": "public",
403 + "table": "relay_environment_credentials"
404 + },
405 + {
406 + "type": "text",
407 + "typeSchema": null,
408 + "notNull": true,
409 + "dimensions": 0,
410 + "default": null,
411 + "generated": null,
412 + "identity": null,
413 + "name": "environment_public_key",
414 + "entityType": "columns",
415 + "schema": "public",
416 + "table": "relay_environment_credentials"
417 + },
418 + {
419 + "type": "varchar(191)",
420 + "typeSchema": null,
421 + "notNull": true,
422 + "dimensions": 0,
423 + "default": null,
424 + "generated": null,
425 + "identity": null,
426 + "name": "credential_hash",
427 + "entityType": "columns",
428 + "schema": "public",
429 + "table": "relay_environment_credentials"
430 + },
431 + {
432 + "type": "varchar(64)",
433 + "typeSchema": null,
434 + "notNull": false,
435 + "dimensions": 0,
436 + "default": null,
437 + "generated": null,
438 + "identity": null,
439 + "name": "revoked_at",
440 + "entityType": "columns",
441 + "schema": "public",
442 + "table": "relay_environment_credentials"
443 + },
444 + {
445 + "type": "varchar(64)",
446 + "typeSchema": null,
447 + "notNull": true,
448 + "dimensions": 0,
449 + "default": null,
450 + "generated": null,
451 + "identity": null,
452 + "name": "created_at",
453 + "entityType": "columns",
454 + "schema": "public",
455 + "table": "relay_environment_credentials"
456 + },
457 + {
458 + "type": "varchar(64)",
459 + "typeSchema": null,
460 + "notNull": true,
461 + "dimensions": 0,
462 + "default": null,
463 + "generated": null,
464 + "identity": null,
465 + "name": "updated_at",
466 + "entityType": "columns",
467 + "schema": "public",
468 + "table": "relay_environment_credentials"
469 + },
470 + {
471 + "type": "varchar(191)",
472 + "typeSchema": null,
473 + "notNull": true,
474 + "dimensions": 0,
475 + "default": null,
476 + "generated": null,
477 + "identity": null,
478 + "name": "user_id",
479 + "entityType": "columns",
480 + "schema": "public",
481 + "table": "relay_environment_links"
482 + },
483 + {
484 + "type": "varchar(191)",
485 + "typeSchema": null,
486 + "notNull": true,
487 + "dimensions": 0,
488 + "default": null,
489 + "generated": null,
490 + "identity": null,
491 + "name": "environment_id",
492 + "entityType": "columns",
493 + "schema": "public",
494 + "table": "relay_environment_links"
495 + },
496 + {
497 + "type": "text",
498 + "typeSchema": null,
499 + "notNull": true,
500 + "dimensions": 0,
501 + "default": "'T3 Environment'",
502 + "generated": null,
503 + "identity": null,
504 + "name": "environment_label",
505 + "entityType": "columns",
506 + "schema": "public",
507 + "table": "relay_environment_links"
508 + },
509 + {
510 + "type": "text",
511 + "typeSchema": null,
512 + "notNull": true,
513 + "dimensions": 0,
514 + "default": null,
515 + "generated": null,
516 + "identity": null,
517 + "name": "environment_public_key",
518 + "entityType": "columns",
519 + "schema": "public",
520 + "table": "relay_environment_links"
521 + },
522 + {
523 + "type": "text",
524 + "typeSchema": null,
525 + "notNull": true,
526 + "dimensions": 0,
527 + "default": null,
528 + "generated": null,
529 + "identity": null,
530 + "name": "endpoint_http_base_url",
531 + "entityType": "columns",
532 + "schema": "public",
533 + "table": "relay_environment_links"
534 + },
535 + {
536 + "type": "text",
537 + "typeSchema": null,
538 + "notNull": true,
539 + "dimensions": 0,
540 + "default": null,
541 + "generated": null,
542 + "identity": null,
543 + "name": "endpoint_ws_base_url",
544 + "entityType": "columns",
545 + "schema": "public",
546 + "table": "relay_environment_links"
547 + },
548 + {
549 + "type": "varchar(32)",
550 + "typeSchema": null,
551 + "notNull": true,
552 + "dimensions": 0,
553 + "default": null,
554 + "generated": null,
555 + "identity": null,
556 + "name": "endpoint_provider_kind",
557 + "entityType": "columns",
558 + "schema": "public",
559 + "table": "relay_environment_links"
560 + },
561 + {
562 + "type": "boolean",
563 + "typeSchema": null,
564 + "notNull": true,
565 + "dimensions": 0,
566 + "default": "true",
567 + "generated": null,
568 + "identity": null,
569 + "name": "notifications_enabled",
570 + "entityType": "columns",
571 + "schema": "public",
572 + "table": "relay_environment_links"
573 + },
574 + {
575 + "type": "boolean",
576 + "typeSchema": null,
577 + "notNull": true,
578 + "dimensions": 0,
579 + "default": "true",
580 + "generated": null,
581 + "identity": null,
582 + "name": "live_activities_enabled",
583 + "entityType": "columns",
584 + "schema": "public",
585 + "table": "relay_environment_links"
586 + },
587 + {
588 + "type": "boolean",
589 + "typeSchema": null,
590 + "notNull": true,
591 + "dimensions": 0,
592 + "default": "false",
593 + "generated": null,
594 + "identity": null,
595 + "name": "managed_tunnels_enabled",
596 + "entityType": "columns",
597 + "schema": "public",
598 + "table": "relay_environment_links"
599 + },
600 + {
601 + "type": "boolean",
602 + "typeSchema": null,
603 + "notNull": true,
604 + "dimensions": 0,
605 + "default": "false",
606 + "generated": null,
607 + "identity": null,
608 + "name": "hold_webhooks_while_offline",
609 + "entityType": "columns",
610 + "schema": "public",
611 + "table": "relay_environment_links"
612 + },
613 + {
614 + "type": "varchar(191)",
615 + "typeSchema": null,
616 + "notNull": false,
617 + "dimensions": 0,
618 + "default": null,
619 + "generated": null,
620 + "identity": null,
621 + "name": "created_by_device_id",
622 + "entityType": "columns",
623 + "schema": "public",
624 + "table": "relay_environment_links"
625 + },
626 + {
627 + "type": "varchar(64)",
628 + "typeSchema": null,
629 + "notNull": false,
630 + "dimensions": 0,
631 + "default": null,
632 + "generated": null,
633 + "identity": null,
634 + "name": "revoked_at",
635 + "entityType": "columns",
636 + "schema": "public",
637 + "table": "relay_environment_links"
638 + },
639 + {
640 + "type": "varchar(64)",
641 + "typeSchema": null,
642 + "notNull": true,
643 + "dimensions": 0,
644 + "default": null,
645 + "generated": null,
646 + "identity": null,
647 + "name": "created_at",
648 + "entityType": "columns",
649 + "schema": "public",
650 + "table": "relay_environment_links"
651 + },
652 + {
653 + "type": "varchar(64)",
654 + "typeSchema": null,
655 + "notNull": true,
656 + "dimensions": 0,
657 + "default": null,
658 + "generated": null,
659 + "identity": null,
660 + "name": "updated_at",
661 + "entityType": "columns",
662 + "schema": "public",
663 + "table": "relay_environment_links"
664 + },
665 + {
666 + "type": "varchar(255)",
667 + "typeSchema": null,
668 + "notNull": true,
669 + "dimensions": 0,
670 + "default": null,
671 + "generated": null,
672 + "identity": null,
673 + "name": "user_id",
674 + "entityType": "columns",
675 + "schema": "public",
676 + "table": "relay_live_activities"
677 + },
678 + {
679 + "type": "varchar(255)",
680 + "typeSchema": null,
681 + "notNull": true,
682 + "dimensions": 0,
683 + "default": null,
684 + "generated": null,
685 + "identity": null,
686 + "name": "device_id",
687 + "entityType": "columns",
688 + "schema": "public",
689 + "table": "relay_live_activities"
690 + },
691 + {
692 + "type": "text",
693 + "typeSchema": null,
694 + "notNull": false,
695 + "dimensions": 0,
696 + "default": null,
697 + "generated": null,
698 + "identity": null,
699 + "name": "activity_push_token",
700 + "entityType": "columns",
701 + "schema": "public",
702 + "table": "relay_live_activities"
703 + },
704 + {
705 + "type": "varchar(64)",
706 + "typeSchema": null,
707 + "notNull": false,
708 + "dimensions": 0,
709 + "default": null,
710 + "generated": null,
711 + "identity": null,
712 + "name": "remote_start_queued_at",
713 + "entityType": "columns",
714 + "schema": "public",
715 + "table": "relay_live_activities"
716 + },
717 + {
718 + "type": "varchar(64)",
719 + "typeSchema": null,
720 + "notNull": false,
721 + "dimensions": 0,
722 + "default": null,
723 + "generated": null,
724 + "identity": null,
725 + "name": "remote_started_at",
726 + "entityType": "columns",
727 + "schema": "public",
728 + "table": "relay_live_activities"
729 + },
730 + {
731 + "type": "varchar(64)",
732 + "typeSchema": null,
733 + "notNull": false,
734 + "dimensions": 0,
735 + "default": null,
736 + "generated": null,
737 + "identity": null,
738 + "name": "ended_at",
739 + "entityType": "columns",
740 + "schema": "public",
741 + "table": "relay_live_activities"
742 + },
743 + {
744 + "type": "jsonb",
745 + "typeSchema": null,
746 + "notNull": false,
747 + "dimensions": 0,
748 + "default": null,
749 + "generated": null,
750 + "identity": null,
751 + "name": "last_aggregate_json",
752 + "entityType": "columns",
753 + "schema": "public",
754 + "table": "relay_live_activities"
755 + },
756 + {
757 + "type": "varchar(64)",
758 + "typeSchema": null,
759 + "notNull": false,
760 + "dimensions": 0,
761 + "default": null,
762 + "generated": null,
763 + "identity": null,
764 + "name": "last_live_activity_delivery_at",
765 + "entityType": "columns",
766 + "schema": "public",
767 + "table": "relay_live_activities"
768 + },
769 + {
770 + "type": "varchar(64)",
771 + "typeSchema": null,
772 + "notNull": true,
773 + "dimensions": 0,
774 + "default": null,
775 + "generated": null,
776 + "identity": null,
777 + "name": "created_at",
778 + "entityType": "columns",
779 + "schema": "public",
780 + "table": "relay_live_activities"
781 + },
782 + {
783 + "type": "varchar(64)",
784 + "typeSchema": null,
785 + "notNull": true,
786 + "dimensions": 0,
787 + "default": null,
788 + "generated": null,
789 + "identity": null,
790 + "name": "updated_at",
791 + "entityType": "columns",
792 + "schema": "public",
793 + "table": "relay_live_activities"
794 + },
795 + {
796 + "type": "varchar(191)",
797 + "typeSchema": null,
798 + "notNull": true,
799 + "dimensions": 0,
800 + "default": null,
801 + "generated": null,
802 + "identity": null,
803 + "name": "user_id",
804 + "entityType": "columns",
805 + "schema": "public",
806 + "table": "relay_managed_endpoint_allocations"
807 + },
808 + {
809 + "type": "varchar(191)",
810 + "typeSchema": null,
811 + "notNull": true,
812 + "dimensions": 0,
813 + "default": null,
814 + "generated": null,
815 + "identity": null,
816 + "name": "environment_id",
817 + "entityType": "columns",
818 + "schema": "public",
819 + "table": "relay_managed_endpoint_allocations"
820 + },
821 + {
822 + "type": "text",
823 + "typeSchema": null,
824 + "notNull": true,
825 + "dimensions": 0,
826 + "default": null,
827 + "generated": null,
828 + "identity": null,
829 + "name": "hostname",
830 + "entityType": "columns",
831 + "schema": "public",
832 + "table": "relay_managed_endpoint_allocations"
833 + },
834 + {
835 + "type": "varchar(191)",
836 + "typeSchema": null,
837 + "notNull": false,
838 + "dimensions": 0,
839 + "default": null,
840 + "generated": null,
841 + "identity": null,
842 + "name": "tunnel_id",
843 + "entityType": "columns",
844 + "schema": "public",
845 + "table": "relay_managed_endpoint_allocations"
846 + },
847 + {
848 + "type": "text",
849 + "typeSchema": null,
850 + "notNull": true,
851 + "dimensions": 0,
852 + "default": null,
853 + "generated": null,
854 + "identity": null,
855 + "name": "tunnel_name",
856 + "entityType": "columns",
857 + "schema": "public",
858 + "table": "relay_managed_endpoint_allocations"
859 + },
860 + {
861 + "type": "varchar(191)",
862 + "typeSchema": null,
863 + "notNull": false,
864 + "dimensions": 0,
865 + "default": null,
866 + "generated": null,
867 + "identity": null,
868 + "name": "dns_record_id",
869 + "entityType": "columns",
870 + "schema": "public",
871 + "table": "relay_managed_endpoint_allocations"
872 + },
873 + {
874 + "type": "varchar(64)",
875 + "typeSchema": null,
876 + "notNull": false,
877 + "dimensions": 0,
878 + "default": null,
879 + "generated": null,
880 + "identity": null,
881 + "name": "ready_at",
882 + "entityType": "columns",
883 + "schema": "public",
884 + "table": "relay_managed_endpoint_allocations"
885 + },
886 + {
887 + "type": "varchar(64)",
888 + "typeSchema": null,
889 + "notNull": false,
890 + "dimensions": 0,
891 + "default": null,
892 + "generated": null,
893 + "identity": null,
894 + "name": "recovery_enabled_at",
895 + "entityType": "columns",
896 + "schema": "public",
897 + "table": "relay_managed_endpoint_allocations"
898 + },
899 + {
900 + "type": "text",
901 + "typeSchema": null,
902 + "notNull": false,
903 + "dimensions": 0,
904 + "default": null,
905 + "generated": null,
906 + "identity": null,
907 + "name": "recovery_environment_public_key",
908 + "entityType": "columns",
909 + "schema": "public",
910 + "table": "relay_managed_endpoint_allocations"
911 + },
912 + {
913 + "type": "varchar(64)",
914 + "typeSchema": null,
915 + "notNull": false,
916 + "dimensions": 0,
917 + "default": null,
918 + "generated": null,
919 + "identity": null,
920 + "name": "tunnel_released_at",
921 + "entityType": "columns",
922 + "schema": "public",
923 + "table": "relay_managed_endpoint_allocations"
924 + },
925 + {
926 + "type": "jsonb",
927 + "typeSchema": null,
928 + "notNull": false,
929 + "dimensions": 0,
930 + "default": null,
931 + "generated": null,
932 + "identity": null,
933 + "name": "origin",
934 + "entityType": "columns",
935 + "schema": "public",
936 + "table": "relay_managed_endpoint_allocations"
937 + },
938 + {
939 + "type": "integer",
940 + "typeSchema": null,
941 + "notNull": true,
942 + "dimensions": 0,
943 + "default": "0",
944 + "generated": null,
945 + "identity": null,
946 + "name": "generation",
947 + "entityType": "columns",
948 + "schema": "public",
949 + "table": "relay_managed_endpoint_allocations"
950 + },
951 + {
952 + "type": "varchar(64)",
953 + "typeSchema": null,
954 + "notNull": true,
955 + "dimensions": 0,
956 + "default": null,
957 + "generated": null,
958 + "identity": null,
959 + "name": "created_at",
960 + "entityType": "columns",
961 + "schema": "public",
962 + "table": "relay_managed_endpoint_allocations"
963 + },
964 + {
965 + "type": "varchar(64)",
966 + "typeSchema": null,
967 + "notNull": true,
968 + "dimensions": 0,
969 + "default": null,
970 + "generated": null,
971 + "identity": null,
972 + "name": "updated_at",
973 + "entityType": "columns",
974 + "schema": "public",
975 + "table": "relay_managed_endpoint_allocations"
976 + },
977 + {
978 + "type": "varchar(191)",
979 + "typeSchema": null,
980 + "notNull": true,
981 + "dimensions": 0,
982 + "default": null,
983 + "generated": null,
984 + "identity": null,
985 + "name": "user_id",
986 + "entityType": "columns",
987 + "schema": "public",
988 + "table": "relay_managed_tunnel_limits"
989 + },
990 + {
991 + "type": "integer",
992 + "typeSchema": null,
993 + "notNull": true,
994 + "dimensions": 0,
995 + "default": null,
996 + "generated": null,
997 + "identity": null,
998 + "name": "max_tunnels",
999 + "entityType": "columns",
1000 + "schema": "public",
1001 + "table": "relay_managed_tunnel_limits"
1002 + },
1003 + {
1004 + "type": "varchar(64)",
1005 + "typeSchema": null,
1006 + "notNull": true,
1007 + "dimensions": 0,
1008 + "default": null,
1009 + "generated": null,
1010 + "identity": null,
1011 + "name": "created_at",
1012 + "entityType": "columns",
1013 + "schema": "public",
1014 + "table": "relay_managed_tunnel_limits"
1015 + },
1016 + {
1017 + "type": "varchar(64)",
1018 + "typeSchema": null,
1019 + "notNull": true,
1020 + "dimensions": 0,
1021 + "default": null,
1022 + "generated": null,
1023 + "identity": null,
1024 + "name": "updated_at",
1025 + "entityType": "columns",
1026 + "schema": "public",
1027 + "table": "relay_managed_tunnel_limits"
1028 + },
1029 + {
1030 + "type": "varchar(255)",
1031 + "typeSchema": null,
1032 + "notNull": true,
1033 + "dimensions": 0,
1034 + "default": null,
1035 + "generated": null,
1036 + "identity": null,
1037 + "name": "user_id",
1038 + "entityType": "columns",
1039 + "schema": "public",
1040 + "table": "relay_mobile_devices"
1041 + },
1042 + {
1043 + "type": "varchar(255)",
1044 + "typeSchema": null,
1045 + "notNull": true,
1046 + "dimensions": 0,
1047 + "default": null,
1048 + "generated": null,
1049 + "identity": null,
1050 + "name": "device_id",
1051 + "entityType": "columns",
1052 + "schema": "public",
1053 + "table": "relay_mobile_devices"
1054 + },
1055 + {
1056 + "type": "text",
1057 + "typeSchema": null,
1058 + "notNull": true,
1059 + "dimensions": 0,
1060 + "default": "'iOS device'",
1061 + "generated": null,
1062 + "identity": null,
1063 + "name": "label",
1064 + "entityType": "columns",
1065 + "schema": "public",
1066 + "table": "relay_mobile_devices"
1067 + },
1068 + {
1069 + "type": "varchar(16)",
1070 + "typeSchema": null,
1071 + "notNull": true,
1072 + "dimensions": 0,
1073 + "default": null,
1074 + "generated": null,
1075 + "identity": null,
1076 + "name": "platform",
1077 + "entityType": "columns",
1078 + "schema": "public",
1079 + "table": "relay_mobile_devices"
1080 + },
1081 + {
1082 + "type": "integer",
1083 + "typeSchema": null,
1084 + "notNull": false,
1085 + "dimensions": 0,
1086 + "default": null,
1087 + "generated": null,
1088 + "identity": null,
1089 + "name": "ios_major_version",
1090 + "entityType": "columns",
1091 + "schema": "public",
1092 + "table": "relay_mobile_devices"
1093 + },
1094 + {
1095 + "type": "integer",
1096 + "typeSchema": null,
1097 + "notNull": false,
1098 + "dimensions": 0,
1099 + "default": null,
1100 + "generated": null,
1101 + "identity": null,
1102 + "name": "android_api_level",
1103 + "entityType": "columns",
1104 + "schema": "public",
1105 + "table": "relay_mobile_devices"
1106 + },
1107 + {
1108 + "type": "varchar(64)",
1109 + "typeSchema": null,
1110 + "notNull": false,
1111 + "dimensions": 0,
1112 + "default": null,
1113 + "generated": null,
1114 + "identity": null,
1115 + "name": "app_version",
1116 + "entityType": "columns",
1117 + "schema": "public",
1118 + "table": "relay_mobile_devices"
1119 + },
1120 + {
1121 + "type": "varchar(255)",
1122 + "typeSchema": null,
1123 + "notNull": false,
1124 + "dimensions": 0,
1125 + "default": null,
1126 + "generated": null,
1127 + "identity": null,
1128 + "name": "bundle_id",
1129 + "entityType": "columns",
1130 + "schema": "public",
1131 + "table": "relay_mobile_devices"
1132 + },
1133 + {
1134 + "type": "varchar(16)",
1135 + "typeSchema": null,
1136 + "notNull": false,
1137 + "dimensions": 0,
1138 + "default": null,
1139 + "generated": null,
1140 + "identity": null,
1141 + "name": "aps_environment",
1142 + "entityType": "columns",
1143 + "schema": "public",
1144 + "table": "relay_mobile_devices"
1145 + },
1146 + {
1147 + "type": "text",
1148 + "typeSchema": null,
1149 + "notNull": false,
1150 + "dimensions": 0,
1151 + "default": null,
1152 + "generated": null,
1153 + "identity": null,
1154 + "name": "push_token",
1155 + "entityType": "columns",
1156 + "schema": "public",
1157 + "table": "relay_mobile_devices"
1158 + },
1159 + {
1160 + "type": "text",
1161 + "typeSchema": null,
1162 + "notNull": false,
1163 + "dimensions": 0,
1164 + "default": null,
1165 + "generated": null,
1166 + "identity": null,
1167 + "name": "push_to_start_token",
1168 + "entityType": "columns",
1169 + "schema": "public",
1170 + "table": "relay_mobile_devices"
1171 + },
1172 + {
1173 + "type": "jsonb",
1174 + "typeSchema": null,
1175 + "notNull": true,
1176 + "dimensions": 0,
1177 + "default": null,
1178 + "generated": null,
1179 + "identity": null,
1180 + "name": "preferences_json",
1181 + "entityType": "columns",
1182 + "schema": "public",
1183 + "table": "relay_mobile_devices"
1184 + },
1185 + {
1186 + "type": "varchar(64)",
1187 + "typeSchema": null,
1188 + "notNull": true,
1189 + "dimensions": 0,
1190 + "default": null,
1191 + "generated": null,
1192 + "identity": null,
1193 + "name": "created_at",
1194 + "entityType": "columns",
1195 + "schema": "public",
1196 + "table": "relay_mobile_devices"
1197 + },
1198 + {
1199 + "type": "varchar(64)",
1200 + "typeSchema": null,
1201 + "notNull": true,
1202 + "dimensions": 0,
1203 + "default": null,
1204 + "generated": null,
1205 + "identity": null,
1206 + "name": "updated_at",
1207 + "entityType": "columns",
1208 + "schema": "public",
1209 + "table": "relay_mobile_devices"
1210 + },
1211 + {
1212 + "type": "varchar(64)",
1213 + "typeSchema": null,
1214 + "notNull": true,
1215 + "dimensions": 0,
1216 + "default": null,
1217 + "generated": null,
1218 + "identity": null,
1219 + "name": "api",
1220 + "entityType": "columns",
1221 + "schema": "public",
1222 + "table": "relay_upstream_api_pauses"
1223 + },
1224 + {
1225 + "type": "bigint",
1226 + "typeSchema": null,
1227 + "notNull": true,
1228 + "dimensions": 0,
1229 + "default": null,
1230 + "generated": null,
1231 + "identity": null,
1232 + "name": "paused_until_ms",
1233 + "entityType": "columns",
1234 + "schema": "public",
1235 + "table": "relay_upstream_api_pauses"
1236 + },
1237 + {
1238 + "nameExplicit": true,
1239 + "columns": [
1240 + {
1241 + "value": "created_at",
1242 + "isExpression": false,
1243 + "asc": true,
1244 + "nullsFirst": false,
1245 + "opclass": null
1246 + }
1247 + ],
1248 + "isUnique": false,
1249 + "where": null,
1250 + "with": "",
1251 + "method": "btree",
1252 + "concurrently": false,
1253 + "name": "idx_relay_delivery_attempts_created",
1254 + "entityType": "indexes",
1255 + "schema": "public",
1256 + "table": "relay_delivery_attempts"
1257 + },
1258 + {
1259 + "nameExplicit": true,
1260 + "columns": [
1261 + {
1262 + "value": "source_job_id",
1263 + "isExpression": false,
1264 + "asc": true,
1265 + "nullsFirst": false,
1266 + "opclass": null
1267 + }
1268 + ],
1269 + "isUnique": true,
1270 + "where": null,
1271 + "with": "",
1272 + "method": "btree",
1273 + "concurrently": false,
1274 + "name": "idx_relay_delivery_attempts_source_job",
1275 + "entityType": "indexes",
1276 + "schema": "public",
1277 + "table": "relay_delivery_attempts"
1278 + },
1279 + {
1280 + "nameExplicit": true,
1281 + "columns": [
1282 + {
1283 + "value": "expires_at",
1284 + "isExpression": false,
1285 + "asc": true,
1286 + "nullsFirst": false,
1287 + "opclass": null
1288 + }
1289 + ],
1290 + "isUnique": false,
1291 + "where": null,
1292 + "with": "",
1293 + "method": "btree",
1294 + "concurrently": false,
1295 + "name": "idx_relay_dpop_proofs_expires_at",
1296 + "entityType": "indexes",
1297 + "schema": "public",
1298 + "table": "relay_dpop_proofs"
1299 + },
1300 + {
1301 + "nameExplicit": true,
1302 + "columns": [
1303 + {
1304 + "value": "credential_hash",
1305 + "isExpression": false,
1306 + "asc": true,
1307 + "nullsFirst": false,
1308 + "opclass": null
1309 + }
1310 + ],
1311 + "isUnique": true,
1312 + "where": null,
1313 + "with": "",
1314 + "method": "btree",
1315 + "concurrently": false,
1316 + "name": "idx_relay_environment_credentials_hash",
1317 + "entityType": "indexes",
1318 + "schema": "public",
1319 + "table": "relay_environment_credentials"
1320 + },
1321 + {
1322 + "nameExplicit": true,
1323 + "columns": [
1324 + {
1325 + "value": "environment_id",
1326 + "isExpression": false,
1327 + "asc": true,
1328 + "nullsFirst": false,
1329 + "opclass": null
1330 + },
1331 + {
1332 + "value": "environment_public_key",
1333 + "isExpression": false,
1334 + "asc": true,
1335 + "nullsFirst": false,
1336 + "opclass": null
1337 + },
1338 + {
1339 + "value": "revoked_at",
1340 + "isExpression": false,
1341 + "asc": true,
1342 + "nullsFirst": false,
1343 + "opclass": null
1344 + }
1345 + ],
1346 + "isUnique": false,
1347 + "where": null,
1348 + "with": "",
1349 + "method": "btree",
1350 + "concurrently": false,
1351 + "name": "idx_relay_environment_credentials_environment_key",
1352 + "entityType": "indexes",
1353 + "schema": "public",
1354 + "table": "relay_environment_credentials"
1355 + },
1356 + {
1357 + "nameExplicit": true,
1358 + "columns": [
1359 + {
1360 + "value": "environment_id",
1361 + "isExpression": false,
1362 + "asc": true,
1363 + "nullsFirst": false,
1364 + "opclass": null
1365 + },
1366 + {
1367 + "value": "revoked_at",
1368 + "isExpression": false,
1369 + "asc": true,
1370 + "nullsFirst": false,
1371 + "opclass": null
1372 + }
1373 + ],
1374 + "isUnique": false,
1375 + "where": null,
1376 + "with": "",
1377 + "method": "btree",
1378 + "concurrently": false,
1379 + "name": "idx_relay_environment_links_environment",
1380 + "entityType": "indexes",
1381 + "schema": "public",
1382 + "table": "relay_environment_links"
1383 + },
1384 + {
1385 + "nameExplicit": true,
1386 + "columns": [
1387 + {
1388 + "value": "activity_push_token",
1389 + "isExpression": false,
1390 + "asc": true,
1391 + "nullsFirst": false,
1392 + "opclass": null
1393 + }
1394 + ],
1395 + "isUnique": true,
1396 + "where": null,
1397 + "with": "",
1398 + "method": "btree",
1399 + "concurrently": false,
1400 + "name": "idx_relay_live_activities_activity_push_token",
1401 + "entityType": "indexes",
1402 + "schema": "public",
1403 + "table": "relay_live_activities"
1404 + },
1405 + {
1406 + "nameExplicit": true,
1407 + "columns": [
1408 + {
1409 + "value": "hostname",
1410 + "isExpression": false,
1411 + "asc": true,
1412 + "nullsFirst": false,
1413 + "opclass": null
1414 + }
1415 + ],
1416 + "isUnique": true,
1417 + "where": null,
1418 + "with": "",
1419 + "method": "btree",
1420 + "concurrently": false,
1421 + "name": "idx_relay_managed_endpoint_allocations_hostname",
1422 + "entityType": "indexes",
1423 + "schema": "public",
1424 + "table": "relay_managed_endpoint_allocations"
1425 + },
1426 + {
1427 + "nameExplicit": true,
1428 + "columns": [
1429 + {
1430 + "value": "tunnel_name",
1431 + "isExpression": false,
1432 + "asc": true,
1433 + "nullsFirst": false,
1434 + "opclass": null
1435 + }
1436 + ],
1437 + "isUnique": true,
1438 + "where": null,
1439 + "with": "",
1440 + "method": "btree",
1441 + "concurrently": false,
1442 + "name": "idx_relay_managed_endpoint_allocations_tunnel_name",
1443 + "entityType": "indexes",
1444 + "schema": "public",
1445 + "table": "relay_managed_endpoint_allocations"
1446 + },
1447 + {
1448 + "nameExplicit": true,
1449 + "columns": [
1450 + {
1451 + "value": "push_token",
1452 + "isExpression": false,
1453 + "asc": true,
1454 + "nullsFirst": false,
1455 + "opclass": null
1456 + }
1457 + ],
1458 + "isUnique": true,
1459 + "where": null,
1460 + "with": "",
1461 + "method": "btree",
1462 + "concurrently": false,
1463 + "name": "idx_relay_mobile_devices_push_token",
1464 + "entityType": "indexes",
1465 + "schema": "public",
1466 + "table": "relay_mobile_devices"
1467 + },
1468 + {
1469 + "nameExplicit": true,
1470 + "columns": [
1471 + {
1472 + "value": "push_to_start_token",
1473 + "isExpression": false,
1474 + "asc": true,
1475 + "nullsFirst": false,
1476 + "opclass": null
1477 + }
1478 + ],
1479 + "isUnique": true,
1480 + "where": null,
1481 + "with": "",
1482 + "method": "btree",
1483 + "concurrently": false,
1484 + "name": "idx_relay_mobile_devices_push_to_start_token",
1485 + "entityType": "indexes",
1486 + "schema": "public",
1487 + "table": "relay_mobile_devices"
1488 + },
1489 + {
1490 + "columns": ["environment_id", "environment_public_key", "thread_id"],
1491 + "nameExplicit": false,
1492 + "name": "relay_agent_activity_rows_pkey",
1493 + "entityType": "pks",
1494 + "schema": "public",
1495 + "table": "relay_agent_activity_rows"
1496 + },
1497 + {
1498 + "columns": ["thumbprint", "jti"],
1499 + "nameExplicit": false,
1500 + "name": "relay_dpop_proofs_pkey",
1501 + "entityType": "pks",
1502 + "schema": "public",
1503 + "table": "relay_dpop_proofs"
1504 + },
1505 + {
1506 + "columns": ["user_id", "environment_id"],
1507 + "nameExplicit": false,
1508 + "name": "relay_environment_links_pkey",
1509 + "entityType": "pks",
1510 + "schema": "public",
1511 + "table": "relay_environment_links"
1512 + },
1513 + {
1514 + "columns": ["user_id", "device_id"],
1515 + "nameExplicit": false,
1516 + "name": "relay_live_activities_pkey",
1517 + "entityType": "pks",
1518 + "schema": "public",
1519 + "table": "relay_live_activities"
1520 + },
1521 + {
1522 + "columns": ["user_id", "environment_id"],
1523 + "nameExplicit": false,
1524 + "name": "relay_managed_endpoint_allocations_pkey",
1525 + "entityType": "pks",
1526 + "schema": "public",
1527 + "table": "relay_managed_endpoint_allocations"
1528 + },
1529 + {
1530 + "columns": ["user_id", "device_id"],
1531 + "nameExplicit": false,
1532 + "name": "relay_mobile_devices_pkey",
1533 + "entityType": "pks",
1534 + "schema": "public",
1535 + "table": "relay_mobile_devices"
1536 + },
1537 + {
1538 + "columns": ["id"],
1539 + "nameExplicit": false,
1540 + "name": "relay_delivery_attempts_pkey",
1541 + "schema": "public",
1542 + "table": "relay_delivery_attempts",
1543 + "entityType": "pks"
1544 + },
1545 + {
1546 + "columns": ["credential_id"],
1547 + "nameExplicit": false,
1548 + "name": "relay_environment_credentials_pkey",
1549 + "schema": "public",
1550 + "table": "relay_environment_credentials",
1551 + "entityType": "pks"
1552 + },
1553 + {
1554 + "columns": ["user_id"],
1555 + "nameExplicit": false,
1556 + "name": "relay_managed_tunnel_limits_pkey",
1557 + "schema": "public",
1558 + "table": "relay_managed_tunnel_limits",
1559 + "entityType": "pks"
1560 + },
1561 + {
1562 + "columns": ["api"],
1563 + "nameExplicit": false,
1564 + "name": "relay_upstream_api_pauses_pkey",
1565 + "schema": "public",
1566 + "table": "relay_upstream_api_pauses",
1567 + "entityType": "pks"
1568 + }
1569 + ],
1570 + "renames": []
1571 +}
infra/relay/package.json
+1
@@ -33,6 +33,7 @@
33 "@cloudflare/workers-types": "^4.20260601.1",
34 "@effect/platform-node": "catalog:",
35 "@effect/vitest": "catalog:",
36 + "@electric-sql/pglite": "0.3.15",
37 "@types/node": "catalog:",
38 "drizzle-kit": "1.0.0-rc.5-ab785fc",
39 "typescript": "catalog:",
infra/relay/src/environments/ManagedEndpointAllocations.test.ts
+27
@@ -74,4 +74,31 @@ describe("ManagedEndpointAllocations", () => {
74 ]);
75 }).pipe(Effect.provide(layerWithDb(fakeDb)));
76 });
77 +
78 + it.effect("teardown claims leave updatedAt to provisioning writes", () => {
79 + // The tunnel limit compares link and allocation updatedAt; a claim that bumps it makes a
80 + // failed revoke cleanup look like a reprovision and keeps holding the user's slot.
81 + const writes: Array<Record<string, unknown>> = [];
82 + const fakeDb = {
83 + update: () => ({
84 + set: (values: Record<string, unknown>) => {
85 + writes.push(values);
86 + return {
87 + where: () => ({ returning: () => Effect.succeed([{ generation: 2 }]) }),
88 + };
89 + },
90 + }),
91 + } as unknown as RelayDb.RelayDb["Service"];
92 +
93 + return Effect.gen(function* () {
94 + const allocations = yield* ManagedEndpointAllocations.ManagedEndpointAllocations;
95 + const key = { userId: "user-1", environmentId: "environment-1", generation: 1 };
96 + yield* allocations.claimDeprovision(key);
97 + yield* allocations.claimRelease({ ...key, tunnelId: "tunnel-1" });
98 + yield* allocations.claimRelease({ ...key, tunnelId: "tunnel-1", markReleased: true });
99 +
100 + expect(writes.map((values) => "updatedAt" in values)).toEqual([false, false, true]);
101 + expect(writes[2]?.["updatedAt"]).toBe(writes[2]?.["tunnelReleasedAt"]);
102 + }).pipe(Effect.provide(layerWithDb(fakeDb)));
103 + });
104 });
infra/relay/src/environments/ManagedEndpointAllocations.ts
+3 -6
@@ -512,9 +512,8 @@ const make = Effect.gen(function* () {
512 const claimed = yield* db
513 .update(relayManagedEndpointAllocations)
514 .set({
515 - updatedAt: now,
515 generation: sql`${relayManagedEndpointAllocations.generation} + 1`,
517 - ...(input.markReleased === true ? { tunnelReleasedAt: now } : {}),
516 + ...(input.markReleased === true ? { tunnelReleasedAt: now, updatedAt: now } : {}),
517 })
518 .where(
519 and(
@@ -585,10 +584,8 @@ const make = Effect.gen(function* () {
584 ) {
585 const claimed = yield* db
586 .update(relayManagedEndpointAllocations)
588 - .set({
589 - updatedAt: DateTime.formatIso(yield* DateTime.now),
590 - generation: sql`${relayManagedEndpointAllocations.generation} + 1`,
591 - })
587 + // Claims move only the generation: the tunnel limit reads updatedAt as the last provisioning write.
588 + .set({ generation: sql`${relayManagedEndpointAllocations.generation} + 1` })
589 .where(
590 and(
591 whereAllocation(input),
infra/relay/src/environments/ManagedEndpointProvider.test.ts
+46
@@ -1406,6 +1406,52 @@ describe("ManagedEndpointProvider", () => {
1406 }).pipe(Effect.provide(layer));
1407 });
1408
1409 + it.effect("marks the tunnel released when removing the allocation fails after deletion", () => {
1410 + const allocationCalls: AllocationCall[] = [];
1411 + const allocations = makeAllocations(allocationCalls);
1412 + const layer = layerProvider(makePersistentTunnelClient(), makeDnsClient(), {
1413 + ...allocations,
1414 + removeClaimed: (input) =>
1415 + Effect.suspend(() => {
1416 + allocationCalls.push({ operation: "removeClaimed", input });
1417 + return Effect.fail(
1418 + new ManagedEndpointAllocations.ManagedEndpointAllocationPersistenceError({
1419 + operation: "remove-claimed",
1420 + stage: "database-request",
1421 + userId: input.userId,
1422 + environmentId: input.environmentId,
1423 + }),
1424 + );
1425 + }),
1426 + });
1427 +
1428 + return Effect.gen(function* () {
1429 + const provider = yield* ManagedEndpointProvider.ManagedEndpointProvider;
1430 + const key = { userId: "user_ABC", environmentId: "env_ABC" } as const;
1431 + yield* provider.provision({
1432 + ...key,
1433 + origin: { localHttpHost: "127.0.0.1", localHttpPort: 3773 },
1434 + });
1435 + const result = yield* Effect.result(provider.deprovision(key));
1436 +
1437 + expect(result._tag).toBe("Failure");
1438 + if (result._tag === "Failure") {
1439 + expect(result.failure).toMatchObject({ stage: "remove-allocation" });
1440 + }
1441 + // The tunnel is gone; without the mark the tunnel limit would keep counting it.
1442 + const claim = allocationCalls.find((call) => call.operation === "claimDeprovision");
1443 + if (claim === undefined) return expect.unreachable("deprovision never claimed the row");
1444 + expect(allocationCalls.at(-1)).toMatchObject({
1445 + operation: "claimRelease",
1446 + input: {
1447 + tunnelId: "tunnel-id",
1448 + generation: (claim.input as { generation: number }).generation + 1,
1449 + markReleased: true,
1450 + },
1451 + });
1452 + }).pipe(Effect.provide(layer));
1453 + });
1454 +
1455 it.effect("treats already deleted remote resources as successfully deprovisioned", () => {
1456 const allocationCalls: AllocationCall[] = [];
1457 const notFound = { _tag: "NotFound" } as const;
infra/relay/src/environments/ManagedEndpointProvider.ts
+26
@@ -882,6 +882,7 @@ const make = Effect.gen(function* () {
882 return false;
883 }
884 const tunnelId = allocation.tunnelId;
885 + let tunnelDeleted = false;
886 const deprovision = Effect.gen(function* () {
887 const dnsRecordId = allocation.dnsRecordId;
888 if (dnsRecordId !== null) {
@@ -909,6 +910,7 @@ const make = Effect.gen(function* () {
910 }),
911 ),
912 );
913 + tunnelDeleted = true;
914 }
915 return yield* allocations
916 .removeClaimed({
@@ -956,6 +958,30 @@ const make = Effect.gen(function* () {
958 }),
959 ),
960 }),
961 + // The rollback keeps the deleted tunnel's id; mark it released so the tunnel limit stops
962 + // counting it. onError also covers an interrupted unlink, which rolls back the same way.
963 + Effect.onError(() =>
964 + tunnelDeleted
965 + ? allocations
966 + .claimRelease({
967 + userId: input.userId,
968 + environmentId: input.environmentId,
969 + tunnelId,
970 + generation: claimedGeneration,
971 + markReleased: true,
972 + })
973 + .pipe(
974 + Effect.catch((error) =>
975 + Effect.logWarning("Failed to mark a deleted managed tunnel released", {
976 + userId: input.userId,
977 + environmentId: input.environmentId,
978 + tunnelId,
979 + error: error.message,
980 + }),
981 + ),
982 + )
983 + : Effect.void,
984 + ),
985 );
986 return Option.getOrElse(removed, () => false);
987 }),
infra/relay/src/environments/ManagedTunnelLimits.test.ts
+188 -16
@@ -1,8 +1,13 @@
1 import { describe, expect, it } from "@effect/vitest";
2 +import * as NodeServices from "@effect/platform-node/NodeServices";
3 import * as Context from "effect/Context";
4 import * as Effect from "effect/Effect";
5 +import * as FileSystem from "effect/FileSystem";
6 import * as Layer from "effect/Layer";
7 import * as Semaphore from "effect/Semaphore";
8 +import { PGlite } from "@electric-sql/pglite";
9 +import { drizzle } from "drizzle-orm/pglite";
10 +import { ne } from "drizzle-orm";
11 import { PgDialect } from "drizzle-orm/pg-core";
12 import type { RelayT3ChatPlan } from "@t3tools/contracts/relay";
13
@@ -11,6 +16,8 @@ import { relayManagedEndpointAllocations } from "../persistence/schema.ts";
16 import * as T3ChatAccounts from "../t3chat/T3ChatAccounts.ts";
17 import * as ManagedTunnelLimits from "./ManagedTunnelLimits.ts";
18
19 +const BACKFILL_MIGRATION = "20261028062605_backfill_withdrawn_tunnel_allocations";
20 +
21 class FakeTransaction extends Context.Service<FakeTransaction, { holdsLock: boolean }>()(
22 "t3code-relay/environments/ManagedTunnelLimits.test/FakeTransaction",
23 ) {}
@@ -19,7 +26,6 @@ interface FakeDbInput {
26 readonly otherSlots: () => number;
27 readonly ownSlot?: boolean;
28 readonly override?: number;
22 - readonly countConditions?: Array<unknown>;
29 }
30
31 // Models Postgres just enough: the advisory lock only works inside a transaction and is held until it ends.
@@ -59,7 +65,6 @@ function makeFakeDb(input: FakeDbInput) {
65 ),
66 };
67 }
62 - input.countConditions?.push(condition);
68 const countsOthers = dialect
69 .sqlToQuery(condition as never)
70 .sql.includes('"relay_managed_endpoint_allocations"."environment_id" <>');
@@ -154,24 +159,191 @@ describe("ManagedTunnelLimits", () => {
159 );
160 });
161
157 - it.effect("pending reservations stop counting once released or withdrawn", () => {
158 - const countConditions: Array<unknown> = [];
162 + it.effect("counts each allocation by its tunnel and link state", () =>
163 + Effect.gen(function* () {
164 + const fs = yield* FileSystem.FileSystem;
165 + const pg = yield* Effect.acquireRelease(
166 + Effect.promise(() => PGlite.create()),
167 + (client) => Effect.promise(() => client.close()),
168 + );
169 + const migrationsDir = new URL("../../migrations/postgres/", import.meta.url).pathname;
170 + const migrations = (yield* fs.readDirectory(migrationsDir)).toSorted();
171 + const applyMigrations = (names: ReadonlyArray<string>) =>
172 + Effect.forEach(names, (name) =>
173 + fs
174 + .readFileString(`${migrationsDir}${name}/migration.sql`)
175 + .pipe(Effect.flatMap((migration) => Effect.promise(() => pg.exec(migration)))),
176 + );
177 + const backfill = migrations.indexOf(BACKFILL_MIGRATION);
178 + expect(backfill).toBeGreaterThan(-1);
179 + yield* applyMigrations(migrations.slice(0, backfill));
180
160 - return Effect.gen(function* () {
161 - yield* reserve({ activeTunnels: 0, plan: "free", countConditions });
181 + const at = (minutes: number) => `2026-10-27T12:${String(minutes).padStart(2, "0")}:00.000Z`;
182 + const cases: ReadonlyArray<{
183 + readonly environmentId: string;
184 + readonly counted: boolean;
185 + readonly allocation: {
186 + readonly tunnelId?: string;
187 + readonly releasedAt?: number;
188 + readonly updatedAt: number;
189 + };
190 + readonly link?: {
191 + readonly revoked?: boolean;
192 + readonly publishOnly?: boolean;
193 + readonly updatedAt: number;
194 + };
195 + }> = [
196 + { environmentId: "pending", counted: true, allocation: { updatedAt: 50 } },
197 + { environmentId: "abandoned-reservation", counted: false, allocation: { updatedAt: 10 } },
198 + {
199 + environmentId: "live",
200 + counted: true,
201 + allocation: { tunnelId: "t", updatedAt: 10 },
202 + link: { updatedAt: 12 },
203 + },
204 + {
205 + environmentId: "released",
206 + counted: false,
207 + allocation: { tunnelId: "t", releasedAt: 20, updatedAt: 20 },
208 + },
209 + {
210 + environmentId: "re-reserved-after-release",
211 + counted: true,
212 + allocation: { tunnelId: "t", releasedAt: 20, updatedAt: 50 },
213 + },
214 + {
215 + environmentId: "revoked",
216 + counted: false,
217 + allocation: { tunnelId: "t", updatedAt: 10 },
218 + link: { revoked: true, updatedAt: 20 },
219 + },
220 + {
221 + environmentId: "revoked-pending",
222 + counted: false,
223 + allocation: { updatedAt: 50 },
224 + link: { revoked: true, updatedAt: 55 },
225 + },
226 + {
227 + environmentId: "publish-only",
228 + counted: false,
229 + allocation: { tunnelId: "t", updatedAt: 10 },
230 + link: { publishOnly: true, updatedAt: 20 },
231 + },
232 + // Reprovisioned after a revoke, but the Worker died before rewriting the link.
233 + {
234 + environmentId: "reprovision-before-link",
235 + counted: true,
236 + allocation: { tunnelId: "t", updatedAt: 20 },
237 + link: { revoked: true, updatedAt: 10 },
238 + },
239 + {
240 + environmentId: "reserved-before-link-rewrite",
241 + counted: true,
242 + allocation: { updatedAt: 50 },
243 + link: { revoked: true, updatedAt: 48 },
244 + },
245 + // Tunnel deleted but the row survived; deprovision marked it released, then a relink stalled.
246 + {
247 + environmentId: "released-after-failed-removal-then-reserved",
248 + counted: false,
249 + allocation: { tunnelId: "t", releasedAt: 20, updatedAt: 30 },
250 + link: { revoked: true, updatedAt: 15 },
251 + },
252 + {
253 + environmentId: "withdrawn-at-provisioning-write",
254 + counted: false,
255 + allocation: { tunnelId: "t", updatedAt: 20 },
256 + link: { revoked: true, updatedAt: 20 },
257 + },
258 + { environmentId: "self", counted: false, allocation: { tunnelId: "t", updatedAt: 10 } },
259 + ];
260 + // Written before the deploy: claimDeprovision bumped updatedAt past the revoke, then cleanup failed.
261 + const legacyCases: typeof cases = [
262 + {
263 + environmentId: "legacy-failed-deprovision",
264 + counted: false,
265 + allocation: { tunnelId: "t", updatedAt: 30 },
266 + link: { revoked: true, updatedAt: 20 },
267 + },
268 + {
269 + environmentId: "legacy-failed-publish-only-deprovision",
270 + counted: false,
271 + allocation: { tunnelId: "t", updatedAt: 30 },
272 + link: { publishOnly: true, updatedAt: 20 },
273 + },
274 + {
275 + environmentId: "legacy-live",
276 + counted: true,
277 + allocation: { tunnelId: "t", updatedAt: 30 },
278 + link: { updatedAt: 20 },
279 + },
280 + ];
281 + const insert = (entries: typeof cases) =>
282 + Effect.forEach(entries, (entry) =>
283 + Effect.promise(async () => {
284 + await pg.query(
285 + `insert into relay_managed_endpoint_allocations
286 + (user_id, environment_id, hostname, tunnel_id, tunnel_name, tunnel_released_at, created_at, updated_at)
287 + values ('user-1', $1::text, $1::text, $2, $1::text, $3, $4, $4)`,
288 + [
289 + entry.environmentId,
290 + entry.allocation.tunnelId ?? null,
291 + entry.allocation.releasedAt === undefined ? null : at(entry.allocation.releasedAt),
292 + at(entry.allocation.updatedAt),
293 + ],
294 + );
295 + const link = entry.link;
296 + if (link !== undefined) {
297 + await pg.query(
298 + `insert into relay_environment_links
299 + (user_id, environment_id, environment_public_key, endpoint_http_base_url,
300 + endpoint_ws_base_url, endpoint_provider_kind, revoked_at, managed_tunnels_enabled,
301 + created_at, updated_at)
302 + values ('user-1', $1, 'key', 'https://e', 'wss://e', 'cloudflare_tunnel', $2, $3, $4, $4)`,
303 + [
304 + entry.environmentId,
305 + link.revoked === true ? at(link.updatedAt) : null,
306 + link.publishOnly !== true,
307 + at(link.updatedAt),
308 + ],
309 + );
310 + }
311 + }),
312 + );
313 + yield* insert(legacyCases);
314 + yield* applyMigrations(migrations.slice(backfill));
315 + yield* insert(cases);
316 + yield* Effect.promise(() =>
317 + pg.query(
318 + `insert into relay_managed_endpoint_allocations
319 + (user_id, environment_id, hostname, tunnel_id, tunnel_name, created_at, updated_at)
320 + values ('user-2', 'other-user', 'other-user', 't', 'other-user', $1, $1)`,
321 + [at(10)],
322 + ),
323 + );
324
163 - expect(countConditions).toHaveLength(1);
164 - const query = new PgDialect().sqlToQuery(countConditions[0] as never);
165 - // claimRelease writes updatedAt = tunnelReleasedAt; a later reserve moves updatedAt past it.
166 - expect(query.sql).toContain(
167 - '"relay_managed_endpoint_allocations"."updated_at" > "relay_managed_endpoint_allocations"."tunnel_released_at"',
325 + const counted = yield* Effect.promise(() =>
326 + drizzle({ client: pg })
327 + .select({ environmentId: relayManagedEndpointAllocations.environmentId })
328 + .from(relayManagedEndpointAllocations)
329 + .where(
330 + ManagedTunnelLimits.countedAllocations({
331 + userId: "user-1",
332 + environmentId: "self",
333 + pendingCutoff: at(45),
334 + environment: ne,
335 + }),
336 + ),
337 );
169 - // A link revoked or made publish-only after the last reservation no longer holds a slot.
170 - expect(query.sql).toContain(
171 - '"relay_environment_links"."updated_at" >= "relay_managed_endpoint_allocations"."updated_at"',
338 +
339 + expect(counted.map((row) => row.environmentId).toSorted()).toEqual(
340 + [...legacyCases, ...cases]
341 + .filter((entry) => entry.counted)
342 + .map((entry) => entry.environmentId)
343 + .toSorted(),
344 );
173 - });
174 - });
345 + }).pipe(Effect.provide(NodeServices.layer)),
346 + );
347
348 it.effect("does not ask T3 Chat while the account is under its cap", () =>
349 Effect.gen(function* () {
infra/relay/src/environments/ManagedTunnelLimits.ts
+53 -43
@@ -47,6 +47,58 @@ export class ManagedTunnelLimitExceeded extends Schema.TaggedError<ManagedTunnel
47 }
48 }
49
50 +const allocation = relayManagedEndpointAllocations;
51 +
52 +const withdrawnLink = (since: typeof allocation.updatedAt) =>
53 + exists(
54 + new QueryBuilder()
55 + .select({ userId: relayEnvironmentLinks.userId })
56 + .from(relayEnvironmentLinks)
57 + .where(
58 + and(
59 + eq(relayEnvironmentLinks.userId, allocation.userId),
60 + eq(relayEnvironmentLinks.environmentId, allocation.environmentId),
61 + or(
62 + isNotNull(relayEnvironmentLinks.revokedAt),
63 + eq(relayEnvironmentLinks.managedTunnelsEnabled, false),
64 + ),
65 + gte(relayEnvironmentLinks.updatedAt, since),
66 + ),
67 + ),
68 + );
69 +
70 +// Counts allocations, not links: a link is written only after its tunnel exists.
71 +// `environment` is `ne` for the user's other environments or `eq` for this one, which relinks without counting itself.
72 +// updatedAt moves only on provisioning writes and release, never on teardown claims, so a link
73 +// withdrawn at or after it means cleanup is owed; an older withdrawal means a relink that never finished.
74 +export const countedAllocations = (input: {
75 + readonly userId: string;
76 + readonly environmentId: string;
77 + readonly pendingCutoff: string;
78 + readonly environment: typeof eq;
79 +}) =>
80 + and(
81 + eq(allocation.userId, input.userId),
82 + input.environment(allocation.environmentId, input.environmentId),
83 + or(
84 + // Reserved but not yet holding a tunnel. Release writes updatedAt = tunnelReleasedAt and
85 + // a relink reserves before rewriting its link, so only a newer reservation counts here.
86 + and(
87 + gt(allocation.updatedAt, input.pendingCutoff),
88 + or(
89 + isNull(allocation.tunnelReleasedAt),
90 + gt(allocation.updatedAt, allocation.tunnelReleasedAt),
91 + ),
92 + not(withdrawnLink(allocation.updatedAt)),
93 + ),
94 + and(
95 + isNotNull(allocation.tunnelId),
96 + isNull(allocation.tunnelReleasedAt),
97 + not(withdrawnLink(allocation.updatedAt)),
98 + ),
99 + ),
100 + );
101 +
102 export class ManagedTunnelLimits extends Context.Service<
103 ManagedTunnelLimits,
104 {
@@ -119,53 +171,11 @@ const make = Effect.gen(function* () {
171 minutes: MANAGED_TUNNEL_PENDING_WINDOW_MINUTES,
172 }),
173 );
122 - // Counts allocations, not links: a link is written only after its tunnel exists.
123 - // Excludes this environment so relinking at the limit stays idempotent.
124 - const allocation = relayManagedEndpointAllocations;
125 - const withdrawnLink = (since?: typeof allocation.updatedAt) =>
126 - exists(
127 - new QueryBuilder()
128 - .select({ userId: relayEnvironmentLinks.userId })
129 - .from(relayEnvironmentLinks)
130 - .where(
131 - and(
132 - eq(relayEnvironmentLinks.userId, allocation.userId),
133 - eq(relayEnvironmentLinks.environmentId, allocation.environmentId),
134 - or(
135 - isNotNull(relayEnvironmentLinks.revokedAt),
136 - eq(relayEnvironmentLinks.managedTunnelsEnabled, false),
137 - ),
138 - since === undefined ? undefined : gte(relayEnvironmentLinks.updatedAt, since),
139 - ),
140 - ),
141 - );
174 const countSlots = (environment: typeof eq) =>
175 db
176 .select({ slots: count() })
177 .from(allocation)
146 - .where(
147 - and(
148 - eq(allocation.userId, input.userId),
149 - environment(allocation.environmentId, input.environmentId),
150 - or(
151 - // Reserved but not yet holding a tunnel. Release writes updatedAt = tunnelReleasedAt and
152 - // a relink reserves before rewriting its link, so only a newer reservation counts here.
153 - and(
154 - gt(allocation.updatedAt, pendingCutoff),
155 - or(
156 - isNull(allocation.tunnelReleasedAt),
157 - gt(allocation.updatedAt, allocation.tunnelReleasedAt),
158 - ),
159 - not(withdrawnLink(allocation.updatedAt)),
160 - ),
161 - and(
162 - isNotNull(allocation.tunnelId),
163 - isNull(allocation.tunnelReleasedAt),
164 - not(withdrawnLink()),
165 - ),
166 - ),
167 - ),
168 - )
178 + .where(countedAllocations({ ...input, pendingCutoff, environment }))
179 .pipe(
180 Effect.map((rows) => rows[0]?.slots ?? 0),
181 Effect.mapError(
pnpm-lock.yaml
+4 -2
@@ -1351,6 +1351,9 @@ importers:
1351 '@effect/vitest':
1352 specifier: 4.0.5
1353 version: 4.0.5(patch_hash=2731c9c3761c595f25ddc625a7d61ca376b389d5b3049ecb837c8940f21cfa02)(@astrojs/compiler-rs@0.5.1)(@tsc-rs/typescript@0.8.0)(@types/node@24.12.4)(@vitest/browser-playwright@5.0.5)(@vitest/ui@5.0.5)(@volar/typescript@2.4.28(@tsc-rs/typescript@0.8.0))(bufferutil@4.1.0)(effect@4.0.5)(esbuild@0.28.2)(jiti@2.7.0)(jsdom@30.1.0(@noble/hashes@1.8.0))(playwright@1.60.0)(terser@5.48.0)(unrun@0.2.39)(utf-8-validate@6.0.6)(yaml@2.9.0)
1354 + '@electric-sql/pglite':
1355 + specifier: 0.3.15
1356 + version: 0.3.15
1357 '@types/node':
1358 specifier: 24.12.4
1359 version: 24.12.4
@@ -13567,8 +13570,7 @@ snapshots:
13570 - vue-tsc
13571 - yaml
13572
13570 - '@electric-sql/pglite@0.3.15':
13571 - optional: true
13573 + '@electric-sql/pglite@0.3.15': {}
13574
13575 '@electron-internal/extract-zip@1.0.5': {}
13576