The best way to control Claude Code, Codex and more from any computer or phone
[codex] Structure agent awareness registration errors (#3328)
Co-authored-by: codex <codex@users.noreply.github.com>
Julius Marminge committed
Jun 20, 2026 at 15:13 UTC
97de7d7e1535ddf485ea8806c639f27e99cfbcb2
2 files changed
+110
-20
apps/mobile/src/features/agent-awareness/remoteRegistration.test.ts
+23
@@ -18,6 +18,7 @@ import { cryptoLayer } from "../cloud/dpop";
18
import { managedRelayClientLayer } from "../cloud/managedRelayLayer";
19
import { makeRelayDeviceRegistrationRequest } from "./registrationPayload";
20
import {
21
+ AgentAwarenessOperationError,
22
__resetAgentAwarenessRemoteRegistrationForTest,
23
refreshActiveLiveActivityRemoteRegistration,
24
refreshAgentAwarenessRegistration,
@@ -267,6 +268,28 @@ describe("makeRelayDeviceRegistrationRequest", () => {
268
}).pipe(Effect.provide(relayTestLayer));
269
});
270
271
+ it.effect("preserves Live Activity push-token lookup failures", () => {
272
+ const cause = new Error("native token lookup failed");
273
+ const activity = {
274
+ getPushToken: vi.fn(() => Promise.reject(cause)),
275
+ addPushTokenListener: vi.fn(),
276
+ };
277
+
278
+ return Effect.gen(function* () {
279
+ const error = yield* Effect.flip(
280
+ registerLiveActivityPushToken({ activity: activity as never }),
281
+ );
282
+
283
+ expect(error).toBeInstanceOf(AgentAwarenessOperationError);
284
+ expect(error).toMatchObject({
285
+ _tag: "AgentAwarenessOperationError",
286
+ operation: "read-live-activity-push-token",
287
+ cause,
288
+ message: "Agent awareness operation read-live-activity-push-token failed.",
289
+ });
290
+ }).pipe(Effect.provide(relayTestLayer));
291
+ });
292
+
293
it.effect(
294
"reports Live Activity token registration as skipped when relay auth is unavailable",
295
() => {
apps/mobile/src/features/agent-awareness/remoteRegistration.ts
+87
-20
@@ -2,6 +2,7 @@ import { addPushToStartTokenListener, type LiveActivity } from "expo-widgets";
2
import Constants from "expo-constants";
3
import * as Notifications from "expo-notifications";
4
import * as Effect from "effect/Effect";
5
+import * as Schema from "effect/Schema";
6
import { Platform } from "react-native";
7
import type { EnvironmentId } from "@t3tools/contracts";
8
import {
@@ -28,6 +29,33 @@ import { resolveCloudPublicConfig } from "../cloud/publicConfig";
29
import { makeRelayDeviceRegistrationRequest } from "./registrationPayload";
30
31
const REMOTE_ACTIVITY_REGISTRATION_RETRY_MS = 15_000;
32
+
33
+const AgentAwarenessOperation = Schema.Literals([
34
+ "read-notification-permissions",
35
+ "read-native-push-token",
36
+ "read-device-registration-relay-token",
37
+ "read-device-unregistration-relay-token",
38
+ "read-live-activity-registration-relay-token",
39
+ "load-device-registration-identifier",
40
+ "load-device-registration-preferences",
41
+ "load-device-unregistration-identifier",
42
+ "read-live-activity-push-token",
43
+ "load-live-activity-registration-identifier",
44
+ "list-active-live-activities",
45
+]);
46
+
47
+export class AgentAwarenessOperationError extends Schema.TaggedErrorClass<AgentAwarenessOperationError>()(
48
+ "AgentAwarenessOperationError",
49
+ {
50
+ operation: AgentAwarenessOperation,
51
+ cause: Schema.Defect(),
52
+ },
53
+) {
54
+ override get message(): string {
55
+ return `Agent awareness operation ${this.operation} failed.`;
56
+ }
57
+}
58
+
59
const environmentConnections = new Map<EnvironmentId, SavedRemoteConnection>();
60
const activityPushTokenListeners = new WeakSet<LiveActivity<AgentActivityProps>>();
61
let pushToStartSubscription: { remove: () => void } | null = null;
@@ -137,14 +165,22 @@ function nativePushTokenRegistration(observedPushToken?: string) {
165
}
166
const permissions = yield* Effect.tryPromise({
167
try: () => Notifications.getPermissionsAsync(),
140
- catch: (error) => error,
168
+ catch: (cause) =>
169
+ new AgentAwarenessOperationError({
170
+ operation: "read-notification-permissions",
171
+ cause,
172
+ }),
173
});
174
if (!permissions.granted) {
175
return { notificationsEnabled: false, pushToken: null };
176
}
177
const token = yield* Effect.tryPromise({
178
try: () => Notifications.getDevicePushTokenAsync(),
147
- catch: (error) => error,
179
+ catch: (cause) =>
180
+ new AgentAwarenessOperationError({
181
+ operation: "read-native-push-token",
182
+ cause,
183
+ }),
184
}).pipe(
185
Effect.tapError((error) =>
186
Effect.sync(() => {
@@ -161,16 +197,19 @@ function nativePushTokenRegistration(observedPushToken?: string) {
197
});
198
}
199
164
-const relayToken = Effect.gen(function* () {
165
- const provider = relayTokenProvider;
166
- if (!provider) {
167
- return null;
168
- }
169
- return yield* Effect.tryPromise({
170
- try: provider,
171
- catch: (error) => error,
200
+const relayToken = (
201
+ operation: "read-device-registration-relay-token" | "read-live-activity-registration-relay-token",
202
+) =>
203
+ Effect.gen(function* () {
204
+ const provider = relayTokenProvider;
205
+ if (!provider) {
206
+ return null;
207
+ }
208
+ return yield* Effect.tryPromise({
209
+ try: provider,
210
+ catch: (cause) => new AgentAwarenessOperationError({ operation, cause }),
211
+ });
212
});
173
-});
213
214
function registerDeviceWithRelay(
215
body: RelayDeviceRegistrationRequest,
@@ -185,7 +224,7 @@ function registerDeviceWithRelay(
224
return;
225
}
226
if (!readRelayConfig()) return;
188
- const token = yield* relayToken;
227
+ const token = yield* relayToken("read-device-registration-relay-token");
228
if (expectedGeneration !== deviceRegistrationGeneration) {
229
logRegistrationDebug("device registration cancelled after auth lookup", {
230
expectedGeneration,
@@ -220,7 +259,11 @@ function unregisterDeviceWithRelay(input: {
259
if (!readRelayConfig()) return;
260
const token = yield* Effect.tryPromise({
261
try: input.tokenProvider,
223
- catch: (error) => error,
262
+ catch: (cause) =>
263
+ new AgentAwarenessOperationError({
264
+ operation: "read-device-unregistration-relay-token",
265
+ cause,
266
+ }),
267
});
268
if (!token) {
269
logRegistrationDebug("relay device unregistration skipped; user is not signed in");
@@ -240,7 +283,7 @@ function registerLiveActivityWithRelay(
283
): Effect.Effect<boolean, unknown, ManagedRelay.ManagedRelayClient> {
284
return Effect.gen(function* () {
285
if (!readRelayConfig()) return false;
243
- const token = yield* relayToken;
286
+ const token = yield* relayToken("read-live-activity-registration-relay-token");
287
if (!token) {
288
logRegistrationDebug("relay live activity registration skipped; user is not signed in");
289
return false;
@@ -381,11 +424,19 @@ function registerDevice(
424
const [deviceId, preferences] = yield* Effect.all([
425
Effect.tryPromise({
426
try: () => loadOrCreateAgentAwarenessDeviceId(),
384
- catch: (error) => error,
427
+ catch: (cause) =>
428
+ new AgentAwarenessOperationError({
429
+ operation: "load-device-registration-identifier",
430
+ cause,
431
+ }),
432
}),
433
Effect.tryPromise({
434
try: () => loadPreferences(),
388
- catch: (error) => error,
435
+ catch: (cause) =>
436
+ new AgentAwarenessOperationError({
437
+ operation: "load-device-registration-preferences",
438
+ cause,
439
+ }),
440
}),
441
]);
442
const pushTokenRegistration = yield* nativePushTokenRegistration(input?.observedPushToken);
@@ -519,7 +570,11 @@ export function unregisterAgentAwarenessDeviceForCurrentUser(
570
return Effect.gen(function* () {
571
const deviceId = yield* Effect.tryPromise({
572
try: () => loadAgentAwarenessDeviceId(),
522
- catch: (error) => error,
573
+ catch: (cause) =>
574
+ new AgentAwarenessOperationError({
575
+ operation: "load-device-unregistration-identifier",
576
+ cause,
577
+ }),
578
});
579
if (!deviceId) {
580
return;
@@ -544,7 +599,11 @@ export function registerLiveActivityPushToken(input: {
599
600
const activityPushToken = yield* Effect.tryPromise({
601
try: () => input.activity.getPushToken(),
547
- catch: (error) => error,
602
+ catch: (cause) =>
603
+ new AgentAwarenessOperationError({
604
+ operation: "read-live-activity-push-token",
605
+ cause,
606
+ }),
607
});
608
if (!activityPushToken) {
609
if (activityPushTokenListeners.has(input.activity)) {
@@ -592,7 +651,11 @@ function registerLiveActivityPushTokenValue(input: {
651
return Effect.gen(function* () {
652
const deviceId = yield* Effect.tryPromise({
653
try: () => loadOrCreateAgentAwarenessDeviceId(),
595
- catch: (error) => error,
654
+ catch: (cause) =>
655
+ new AgentAwarenessOperationError({
656
+ operation: "load-live-activity-registration-identifier",
657
+ cause,
658
+ }),
659
});
660
const registered = yield* registerLiveActivityWithRelay({
661
deviceId,
@@ -633,7 +696,11 @@ export function refreshActiveLiveActivityRemoteRegistration(): Effect.Effect<
696
697
const activities = yield* Effect.try({
698
try: () => AgentActivity.getInstances(),
636
- catch: (error) => error,
699
+ catch: (cause) =>
700
+ new AgentAwarenessOperationError({
701
+ operation: "list-active-live-activities",
702
+ cause,
703
+ }),
704
}).pipe(
705
Effect.catch((error) =>
706
Effect.sync(() => {