The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 12:15 UTC
fix(relay): slow APNs requests no longer delay other devices (#32183)
Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Sudharsan committed
Nov 15, 2026 at 10:01 UTC
0fa8e5afe7cfce0b59710fef9c58f33b843eebdd
4 files changed
+135
-18
infra/relay/src/agentActivity/AgentActivityPublisher.test.ts
+1
@@ -116,6 +116,7 @@ function makeApnsDeliveries(
116
overrides: Partial<ApnsDeliveries.ApnsDeliveries["Service"]> = {},
117
): ApnsDeliveries.ApnsDeliveries["Service"] {
118
return {
119
+ processBatch: () => Effect.void,
120
sendForTarget: () => Effect.succeed(null),
121
sendLiveActivity: () =>
122
Effect.succeed({
infra/relay/src/agentActivity/ApnsDeliveries.test.ts
+98
-8
@@ -8,9 +8,13 @@ import * as NodeCryptoLayer from "@effect/platform-node/NodeCrypto";
8
import { describe, expect, it } from "@effect/vitest";
9
// @effect-diagnostics-next-line nodeBuiltinImport:off -- Effect's Crypto has no generateKeyPairSync.
10
import * as NodeCrypto from "node:crypto";
11
+import * as Deferred from "effect/Deferred";
12
import * as Effect from "effect/Effect";
13
+import * as Fiber from "effect/Fiber";
14
import * as Layer from "effect/Layer";
15
import * as Redacted from "effect/Redacted";
16
+import * as Stream from "effect/Stream";
17
+import * as TestClock from "effect/testing/TestClock";
18
import { FetchHttpClient, HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http";
19
20
import {
@@ -745,6 +749,81 @@ describe("ApnsDeliveries", () => {
749
);
750
});
751
752
+ it.effect("delivers to other devices without reordering a device's queued jobs", () =>
753
+ Effect.gen(function* () {
754
+ const attempts: Array<DeliveryAttempts.DeliveryAttemptInput> = [];
755
+ const requests: Array<string> = [];
756
+ let firstDeviceRequest = true;
757
+ const firstStarted = yield* Deferred.make<void>();
758
+ const otherDeviceDelivered = yield* Deferred.make<void>();
759
+ const devices = [
760
+ { ...target, push_token: "first-token" },
761
+ { ...target, device_id: "device-2", push_token: "other-token" },
762
+ ];
763
+ const jobs = yield* Effect.forEach([devices[0]!, devices[0]!, devices[1]!], (device, i) =>
764
+ signApnsDeliveryJob({
765
+ secret: signingConfig.apnsDeliveryJobSigningSecret,
766
+ payload: makeApnsDeliveryJobPayload({
767
+ kind: "push_notification",
768
+ userId: device.user_id,
769
+ deviceId: device.device_id,
770
+ token: device.push_token,
771
+ aggregate: null,
772
+ notification: {
773
+ title: "Thread",
774
+ body: "Done",
775
+ environmentId: "env",
776
+ threadId: `thread-${i}`,
777
+ deepLink: "/",
778
+ },
779
+ createdAt: "1970-01-01T00:00:00.000Z",
780
+ expiresAt: "1970-01-01T00:10:00.000Z",
781
+ jobId: `job-${i}`,
782
+ }),
783
+ }),
784
+ );
785
+ const batch = yield* Effect.flatMap(ApnsDeliveries.ApnsDeliveries, (deliveries) =>
786
+ deliveries.processBatch(Stream.fromIterable(jobs.map((body) => ({ body })))),
787
+ ).pipe(
788
+ Effect.provide(
789
+ layerFor({
790
+ attempts,
791
+ currentTargets: devices,
792
+ config: signingConfig,
793
+ execute: (request) =>
794
+ Effect.gen(function* () {
795
+ const token = request.url.split("/").at(-1)!;
796
+ requests.push(token);
797
+ if (request.url.endsWith("/other-token")) {
798
+ yield* Deferred.succeed(otherDeviceDelivered, undefined);
799
+ } else if (firstDeviceRequest) {
800
+ firstDeviceRequest = false;
801
+ yield* Deferred.succeed(firstStarted, undefined);
802
+ yield* Deferred.await(otherDeviceDelivered);
803
+ }
804
+ return HttpClientResponse.fromWeb(request, new Response("", { status: 200 }));
805
+ }),
806
+ }),
807
+ ),
808
+ Effect.forkChild,
809
+ );
810
+ yield* Deferred.await(firstStarted);
811
+ yield* TestClock.adjust("10 seconds");
812
+ yield* Fiber.join(batch);
813
+
814
+ expect(requests).toHaveLength(3);
815
+ expect(requests.filter((token) => token === "first-token")).toHaveLength(2);
816
+ expect(requests.indexOf("other-token")).toBeLessThan(requests.lastIndexOf("first-token"));
817
+ expect(attempts).toHaveLength(3);
818
+ expect(
819
+ attempts
820
+ .filter((attempt) => attempt.deviceId === target.device_id)
821
+ .map((attempt) => attempt.sourceJobId),
822
+ ).toEqual(["job-0", "job-1"]);
823
+ expect(attempts.every((attempt) => attempt.apnsStatus === 200)).toBe(true);
824
+ }).pipe(Effect.provide(NodeCryptoLayer.layerMac)),
825
+ );
826
+
827
it.effect("skips duplicate signed queue jobs before calling APNs", () => {
828
const attempts: Array<DeliveryAttempts.DeliveryAttemptInput> = [];
829
let executeCount = 0;
@@ -926,7 +1005,7 @@ describe("ApnsDeliveries", () => {
1005
);
1006
});
1007
929
- it.effect("retries signed queue jobs that are already claimed but not completed", () => {
1008
+ it.effect("retries an in-flight device's jobs without cancelling other devices", () => {
1009
const attempts: Array<DeliveryAttempts.DeliveryAttemptInput> = [];
1010
let executeCount = 0;
1011
const payload = makeApnsDeliveryJobPayload({
@@ -946,10 +1025,18 @@ describe("ApnsDeliveries", () => {
1025
expiresAt: "1970-01-01T00:10:00.000Z",
1026
jobId: "job-push-in-flight",
1027
});
949
- const signed = signApnsDeliveryJob({
950
- secret: config.apnsDeliveryJobSigningSecret,
951
- payload,
952
- });
1028
+ const signed = Effect.forEach(
1029
+ [
1030
+ payload,
1031
+ { ...payload, jobId: "job-push-after-in-flight" },
1032
+ {
1033
+ ...payload,
1034
+ jobId: "job-push-other-device",
1035
+ target: { ...payload.target, deviceId: "device-2", token: "other-token" },
1036
+ },
1037
+ ],
1038
+ (payload) => signApnsDeliveryJob({ secret: config.apnsDeliveryJobSigningSecret, payload }),
1039
+ );
1040
const execute = (request: HttpClientRequest.HttpClientRequest) =>
1041
Effect.sync(() => {
1042
executeCount += 1;
@@ -958,19 +1045,22 @@ describe("ApnsDeliveries", () => {
1045
1046
return Effect.gen(function* () {
1047
const deliveries = yield* ApnsDeliveries.ApnsDeliveries;
961
- const result = yield* Effect.exit(deliveries.processSignedJob(yield* signed));
1048
+ const result = yield* Effect.exit(
1049
+ deliveries.processBatch(Stream.fromIterable((yield* signed).map((body) => ({ body })))),
1050
+ );
1051
1052
expect(result._tag).toBe("Failure");
1053
if (result._tag === "Failure") {
1054
expect(result.cause.toString()).toContain("ApnsDeliveryJobClaimInFlight");
1055
}
967
- expect(executeCount).toBe(0);
968
- expect(attempts).toEqual([]);
1056
+ expect(executeCount).toBe(1);
1057
+ expect(attempts).toMatchObject([{ sourceJobId: "job-push-other-device", apnsStatus: 200 }]);
1058
}).pipe(
1059
Effect.provide(
1060
layerFor({
1061
attempts,
1062
sourceJobClaims: new Map([["job-push-in-flight", "in_flight"]]),
1063
+ currentTargets: [{ ...target, device_id: "device-2", push_token: "other-token" }],
1064
config: signingConfig,
1065
execute,
1066
}),
infra/relay/src/agentActivity/ApnsDeliveries.ts
+33
@@ -10,6 +10,7 @@ import {
10
RelayAgentAwarenessPreferences as RelayAgentAwarenessPreferencesSchema,
11
RelayDeliveryKind as RelayDeliveryKindSchema,
12
} from "@t3tools/contracts/relay";
13
+import * as Arr from "effect/Array";
14
import * as Context from "effect/Context";
15
import * as DateTime from "effect/DateTime";
16
import * as Mac from "effect/crypto/Mac";
@@ -18,6 +19,7 @@ import * as Layer from "effect/Layer";
19
import * as Option from "effect/Option";
20
import * as Redacted from "effect/Redacted";
21
import * as Schema from "effect/Schema";
22
+import * as Stream from "effect/Stream";
23
24
import {
25
isExpiredAgentActivityState,
@@ -132,6 +134,7 @@ const decodeRelayAgentAwarenessPreferencesJson = Schema.decodeUnknownOption(
134
Schema.fromJsonString(RelayAgentAwarenessPreferencesSchema),
135
);
136
const decodeSignedApnsDeliveryJob = Schema.decodeUnknownEffect(SignedApnsDeliveryJob);
137
+const decodeSignedApnsDeliveryJobOption = Schema.decodeUnknownOption(SignedApnsDeliveryJob);
138
139
function parseAggregate(value: string | null): RelayAgentActivityAggregateState | null {
140
if (!value) {
@@ -490,6 +493,9 @@ export class ApnsDeliveries extends Context.Service<
493
readonly processSignedJob: (
494
body: unknown,
495
) => Effect.Effect<RelayDeliveryResult, ApnsDeliveryError>;
496
+ readonly processBatch: (
497
+ stream: Stream.Stream<{ readonly body: unknown }>,
498
+ ) => Effect.Effect<void, Arr.NonEmptyArray<ApnsDeliveryError>>;
499
readonly sendPushNotification: (input: {
500
readonly target: LiveActivityDeliveryTarget;
501
readonly token: string;
@@ -1003,10 +1009,37 @@ const make = Effect.gen(function* () {
1009
}).pipe(withSpanAttributes({ "user.id": payload.target.userId }));
1010
});
1011
1012
+ const processBatch: ApnsDeliveries["Service"]["processBatch"] = Effect.fn(
1013
+ "relay.apn_delivery_queue.process_batch",
1014
+ )(function* (stream) {
1015
+ const messages = yield* Stream.runCollect(stream);
1016
+ const groups = Arr.groupBy(messages, (message) => {
1017
+ const job = decodeSignedApnsDeliveryJobOption(message.body);
1018
+ return Option.isSome(job)
1019
+ ? JSON.stringify([job.value.payload.target.userId, job.value.payload.target.deviceId])
1020
+ : "";
1021
+ });
1022
+ // Finish other devices before surfacing failures so their in-flight sends aren't cancelled.
1023
+ yield* Effect.validate(
1024
+ Object.values(groups),
1025
+ (group) =>
1026
+ Effect.forEach(
1027
+ group,
1028
+ (message) =>
1029
+ processSignedJob(message.body).pipe(
1030
+ Effect.withSpan("relay.apn_delivery_queue.process_message"),
1031
+ ),
1032
+ { discard: true },
1033
+ ),
1034
+ { concurrency: 10, discard: true },
1035
+ );
1036
+ });
1037
+
1038
return ApnsDeliveries.of({
1039
sendLiveActivity,
1040
sendPushNotification,
1041
processSignedJob,
1042
+ processBatch,
1043
sendForTarget: Effect.fnUntraced(function* (input) {
1044
if (!config.apns) return null;
1045
const delivery = chooseDelivery(input);
infra/relay/src/worker.ts
+3
-10
@@ -448,16 +448,9 @@ export const layer = Api.make(
448
deadLetterQueue: apnsDeliveryDeadLetterQueue.queueName as unknown as string,
449
},
450
(stream) =>
451
- stream.pipe(
452
- Stream.withSpan("relay.apn_delivery_queue.process_batch"),
453
- Stream.runForEach((message) =>
454
- ApnsDeliveries.ApnsDeliveries.pipe(
455
- Effect.flatMap((deliveries) => deliveries.processSignedJob(message.body)),
456
- Effect.withSpan("relay.apn_delivery_queue.process_message"),
457
- ),
458
- ),
459
- Effect.provide(layerRuntime),
460
- ),
451
+ Effect.flatMap(ApnsDeliveries.ApnsDeliveries, (deliveries) =>
452
+ deliveries.processBatch(stream),
453
+ ).pipe(Effect.provide(layerRuntime)),
454
);
455
456
yield* Cloudflare.Queues.consumeQueueMessages<unknown>(